我在我的集合视图中使用以下代码:cellForItemAtIndexPath
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = nil;
if ([ImageArray count] >0){
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void) {
NSData *data0 = [NSData dataWithContentsOfURL: [NSURL URLWithString:[ImageArray objectAtIndex:indexPath.row]]];
UIImage *image = [UIImage imageWithData: data0];
dispatch_sync(dispatch_get_main_queue(), ^(void) {
recipeImageView.image = image;
});
});
}
[spinnerShow stopAnimating];
cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
return cell;
}
问题是,如果我滚动集合视图,图像会闪烁。为什么会这样?当我滚动图像时,如何防止图像闪烁?
答案 0 :(得分:3)
我认为根本原因是加载以下2行图像的成本:
NSData *data0 = [NSData dataWithContentsOfURL: [NSURL URLWithString:[ImageArray objectAtIndex:indexPath.row]]];
UIImage *image = [UIImage imageWithData: data0];
我的解决方案是使用AFNetworking加载图片处理,请参考链接:https://github.com/AFNetworking/AFNetworking
安装AFNetworking后,您只需:
if ([ImageArray count] >0){
[recipeImageView setImageWithURL:[NSURL URLWithString:[ImageArray objectAtIndex:indexPath.row]] placeholderImage:nil];
}
希望你喜欢这个!