【发布时间】:2016-06-12 14:35:39
【问题描述】:
UICollectionView 的每个单元格都有一个 UIImageView,我需要加载它们,以便在您滚动浏览集合时它们正确显示,并且每个图像在其索引路径的单元格上加载一次。
每张图片都是从 Kinvey 下载的。
下载每张图片的代码:
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, cell.bounds.size.width, cell.bounds.size.width)];
[cell addSubview:imageView];
[KCSFileStore downloadData:@"id for item at index path"] completionBlock:^(NSArray *downloadedResources, NSError *error) {
if (error == nil) {
KCSFile* file = downloadedResources[0];
dispatch_async(dispatch_get_main_queue(), ^(void){
imageView.image = [UIImage imageWithData:file.data];
});
NSLog(@"Downloaded.");
} else {
NSLog(@"Error: %@", error.localizedDescription);
}
} progressBlock:nil];
“dispatch_async(dispatch_get_main_queue(), ^(void)...”是我试图解决异步下载图像的问题,但没有奏效。
提前致谢。
【问题讨论】:
-
您到底想做什么?在后台下载图片,滚动时应该会出现,不是吗?
-
标签: objective-c uicollectionview lazy-loading kinvey