【发布时间】:2016-04-02 06:43:39
【问题描述】:
我有一个代码可以下载大约 1000 张图像,还可以使用 NSCache 执行缓存并在 UICollectionView 中显示图像。它可以工作,但是当 UICollectionView 滚动得非常快并且互联网速度很慢时,会收到内存警告。那么这里的问题可能是什么?以下是我的代码:提前致谢。
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
ProductCollectionViewCell *cell = (ProductCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
//set image,title of product
cell.productLabel.text = [self.productNameArray objectAtIndex:indexPath.row];
UIImage *image = [self.imgCache getCachedImageForKey:[self.imgUrlArray objectAtIndex:indexPath.row]];
if (image.size.width>0) {
[cell.productImage setImage:image];
}
else {
[cell.productImage setImage:nil];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(void){
NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[self.imgUrlArray objectAtIndex:indexPath.row]]];
UIImage *img = [UIImage imageWithData:imgData];
[cell.productImage setImage:nil];
dispatch_async(dispatch_get_main_queue(), ^{
if (imgData!=nil) {
[cell.productImage setImage:img];
[self.imgCache cacheImage:img forKey:[self.imgUrlArray objectAtIndex:indexPath.row]];
if ([self.imgDataArray count] <= [self.productNameArray count]) {
[self.imgDataArray addObject:imgData];
}
}
});
if ([self.imgDataArray count] == [self.productNameArray count]){
[self.productsView.collectionView reloadData];
}
});
}
return cell;
}
【问题讨论】:
标签: ios objective-c uicollectionview