Ios 按钮的活动指示器
我有一个从互联网下载的图像网格视图。当我点击一个按钮进入该视图时需要时间,第一个屏幕被冻结,第二个视图仅在下载所有图像时显示。我如何才能在我必须等待的时间上设置活动指示器?将活动视图添加到您的单元格/视图中,如下所示:Ios 按钮的活动指示器,ios,objective-c,gridview,uiactivityindicatorview,Ios,Objective C,Gridview,Uiactivityindicatorview,我有一个从互联网下载的图像网格视图。当我点击一个按钮进入该视图时需要时间,第一个屏幕被冻结,第二个视图仅在下载所有图像时显示。我如何才能在我必须等待的时间上设置活动指示器?将活动视图添加到您的单元格/视图中,如下所示: UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; spinne
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
spinner.frame = CGRectMake(0, 0, 24, 24);
[self.view addSubview:spinner];
[spinner startAnimating];
调用[spinner stop animating]当NSURLConnection委托方法通知您它们已完成时,将其从superView中删除
还要确保只在主线程上调用上面的代码(与UI交互)。首先,您不需要放置活动指示器并等待静态下载所有图像。。试想一下,如果有1000张图片,用户必须等到下载完成后才能开始使用您的应用程序
我假设这个gridView是tableView,因为在移动设计世界中
gridView不存在
视图未加载
- (void)viewDidLoad {
[super viewDidLoad];
array = [[NSMutableArray alloc]initWithObjects:@"http://www.gettyimages.co.uk/CMS/StaticContent/1391099215267_hero2.jpg", @"http://1hdwallpapers.com/wallpapers/creativity_water_spray_drops_flower_rose_desktop_images.jpg" , @"http://i.telegraph.co.uk/multimedia/archive/02848/wellcome-photo-13__2848386k.jpg" ,@"http://www.gettyimages.co.uk/CMS/StaticContent/1391099215267_hero2.jpg" , @"http://www.hdwallpapersimages.com/wp-content/uploads/images/Frozen-Logo-Symbol-HD-Images.jpg" , @"http://images.visitcanberra.com.au/images/canberra_hero_image.jpg" , @"http://www.desktopwallpaperhd.net/wallpapers/21/7/wallpaper-sunset-images-back-217159.jpg" , nil];
tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height - 20) style:UITableViewStylePlain];
// DO NOT FORGET UITableViewDelegate, UITableViewDataSource protocols
[tableView setDelegate:self];
[tableView setDataSource:self];
[self.view addSubview:tableView];
}
行数:
单元配置
现在使用getImagesAsync方法
现在,当您打开应用程序时,您应该看到图像是如何一个接一个异步下载的,并且您仍然可以与应用程序交互。
下载完成后,tableView的UI将使用以下方法自动更新:
[单元格设置需要删除]
这是所有现代应用程序的构建方式,如facebook、twitter、google+等。尝试使用NSURLConnection委托方法并将UIActivityIndicatorView添加为子视图。您应该异步下载图像。类似这样的情况:当我按下按钮时,屏幕冻结,这是我的问题。下一个视图仅在下载所有图像时显示。您如何获取图像?这听起来像是在阻止主线程,这就是UI冻结的原因。您需要在后台线程上执行图像获取(使用带有NSURLConnection的Grand Central Dispatch或(更好的)后台NSURLSession)。然后,您将能够显示UIActivityIndicatorView,以便在用户等待图像下载时向用户突出显示正在后台完成的工作。我希望这是清楚的。我使用的是集合视图。我相信这段代码也会帮助您使用集合视图。。我只是从来没有用过collection view来做一个例子,很抱歉我没有理解,一切都好。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//You need a global declaration for cell
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellID"];
[self getImagesAsyncFor:indexPath];
return cell;
}
-(void)getImagesAsyncFor:(NSIndexPath *)indexPath_ {
// Fetch using GCD
dispatch_queue_t downloadThumbnailQueue = dispatch_queue_create("Get Photo Thumbnail", NULL);
dispatch_async(downloadThumbnailQueue, ^{
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[array objectAtIndex:indexPath_.row]]]];
dispatch_async(dispatch_get_main_queue(), ^{
cell = [self->tableView cellForRowAtIndexPath:indexPath_]; // create a copy of the cell to avoid keeping a strong pointer to "cell" since that one may have been reused by the time the block is ready to update it.
if (cell != nil) {
[cell.imageView setImage:image];
[cell setNeedsLayout];
}
});
});
}