我正在開發一個帶有示例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似乎比較慢,但它的工作方式與我期望的那樣可以下載圖像! –