我有一个UITableView
,每个cell
中都有一个图像。当我单击cell
时,将下载图像。我要显示活动指示器,直到未下载图像为止。
这里是代码
TableView代码
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(PicturePerformGesture:)];
[cell.oMessageImg setUserInteractionEnabled:YES];
}
PicturePerformGesture代码
UIActivityIndicatorView *iActivity=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[iActivity startAnimating];
VideoData =[NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:videoUrl] returningResponse:nil error:nil];
[VideoData writeToFile:fullPath atomically:true];
[iActivity stopAnimating];
[此时创建图像单元时,需要检查是否下载了图像,如果是,可以点击图像,否则在其上启动ActivityIndicator,并且在完成图像下载时,可以停止ActivityIndicator以获得最佳图像下载体验,请使用SDWebImage 3rd party库并获得所有图像状态,如下所述。
使用补全框:
// Here we use the new provided sd_setImageWithURL: method to load the web image
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://www.example.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
... completion code here ...
// Add Your Stop Activity Code here.
}];
获得图像下载进度的另一种方法,使用下面的代码块:
SDWebImageDownloader *downloader = [SDWebImageDownloader sharedDownloader];
[downloader downloadImageWithURL:imageURL
options:0
progress:^(NSInteger receivedSize, NSInteger expectedSize) {
// progression tracking code
}
completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
if (image && finished) {
// do something with image
// Assign Image to ImageView and stop activity Indicator View
}
}];
SDWebImage链接:https://github.com/rs/SDWebImage
或者您可以使用自己的代码在GCD
dispatch_async
的帮助下下载图像,如下所述。
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^(void) {
// You may want to cache this explicitly instead of reloading every time.
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[self.loader.parsedData[indexPath.row] objectForKey:@"imageLR"]]];
UIImage* image = [[UIImage alloc] initWithData:imageData];
dispatch_async(dispatch_get_main_queue(), ^{
// Capture the indexPath variable, not the cell variable, and use that
UITableViewCell *blockCell = [tableView cellForRowAtIndexPath:indexPath];
blockCell.imageView.image = image;
[blockCell setNeedsLayout];
//Stop Your ActivityIndicator View hare
});
});
希望它会帮助您。
使用此方法在图像下载块代码之前开始动画。
[myActivityIndicator startAnimating];
并使用像这样的iOS块停止在图像下载中...
SDWebImageDownloader *downloader = [SDWebImageDownloader sharedDownloader];
[downloader downloadImageWithURL:imageURL
options:0
progress:^(NSInteger receivedSize, NSInteger expectedSize) {
// progression tracking code
}
completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
[myActivityIndicator startAnimating];
if (image && finished) {
}
}];