CollectionView 图像在滚动时闪烁 [英] CollectionView images are flashing while scrolling
本文介绍了CollectionView 图像在滚动时闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在我的 collectionView 中使用以下代码: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;
}
问题是,如果我滚动集合视图,图像会闪烁.为什么呢?滚动时如何防止图像闪烁?
The problem is that, the images are flashing if I scroll the collection view. Why is that so? How can I be able to prevent images from flashing when Im scrolling it?
推荐答案
我认为根本原因是加载下面 2 行图像的成本:
I think the root reason is the cost for loading image of 2 line below :
NSData *data0 = [NSData dataWithContentsOfURL: [NSURL URLWithString:[ImageArray objectAtIndex:indexPath.row]]];
UIImage *image = [UIImage imageWithData: data0];
我的解决方案是使用 AFNetworking 加载图片过程,请参考链接:https://github.com/AFNetworking/AFNetworking
My solution is use AFNetworking for loading image process, please reference to link : https://github.com/AFNetworking/AFNetworking
安装 AFNetworking 后,您只需:
After install AFNetworking, you simply :
if ([ImageArray count] >0){
[recipeImageView setImageWithURL:[NSURL URLWithString:[ImageArray objectAtIndex:indexPath.row]] placeholderImage:nil];
}
希望你喜欢这个!
这篇关于CollectionView 图像在滚动时闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文