我正在开发一个带有示例UICollectionView的应用程序,该应用程序应该加载它从互联网上下载的一堆图标。所以基本上服务器会检索一个包含JSON文件中每个图标的URL的列表,然后应用程序解析它,然后每个单元格下载相应的图像。UICollectionView单元格图像错误
这种方法的问题似乎是,如果用户在图像下载时开始滚动,用户将开始以错误的顺序看到图像!这就像UICollectionView试图“帮助我”一样,它会呈现出错的地方!
我见过很多关于这方面的线索,我尝试了大部分建议,但迄今为止运气很好。有人看到这样的事情?
这是cellForItemAtIndexPath的样子:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
PeqImage *image = [self.responseIcons objectAtIndex:indexPath.row];
PeqCustomCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"PeqCustomCell" forIndexPath:indexPath];
cell.peqImage = image;
cell.imageName = [NSString stringWithFormat:@"%@.png",image.assetId];
NSString *theImageUrl = [NSString stringWithFormat:@"http://www.XXXX.com/pb/images/uncompressed/%ld.png",(long)indexPath.row];
[cell downloadImage:[NSURL URLWithString:theImageUrl]];
return cell;
}
这是下载算法的样子(使用的UIImageView + AFNetworking):
- (void) downloadImageWithUrl:(NSURL*) url completion:(PeqCustomCellCompletionBlock)completionBlock
{UIImage *placeholder = [UIImage imageNamed:@"placeholder"];
NSURLRequest *jpegURLRequest = [NSURLRequest requestWithURL:url];
self.imageUrl = url.absoluteString;
[self.imageView setImageWithURLRequest:jpegURLRequest
placeholderImage:placeholder
success:^(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, UIImage *image) {
image = [self normalizeImage:image];
completionBlock(image);
}
failure:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, NSError * _Nonnull error) {
NSLog(@"Error downloading image from network");
}];
}
谢谢你的回答。我做了诡计!我正在使用另一个库 (UIImageView + AFNetworking),但尽管SDWebImage似乎比较慢,但它的工作方式与我期望的那样可以下载图像! –