Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios CollectionView图像在滚动时闪烁_Ios_Objective C_Uicollectionview - Fatal编程技术网

Ios CollectionView图像在滚动时闪烁

Ios CollectionView图像在滚动时闪烁,ios,objective-c,uicollectionview,Ios,Objective C,Uicollectionview,我在我的collectionView中使用了以下代码:cellForItemAtIndexPath: - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { static NSString *identifier = @"Cell"; UICollectio

我在我的
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;
}

问题是,如果我滚动集合视图,图像会闪烁。为什么会这样?我怎样才能防止在Im滚动图像时图像闪烁?

我认为根本原因是加载下面两行图像的成本:

NSData *data0 = [NSData dataWithContentsOfURL: [NSURL URLWithString:[ImageArray objectAtIndex:indexPath.row]]];
UIImage *image = [UIImage imageWithData: data0];
我的解决方案是使用AFNetworking加载图像处理,请参考链接:

安装AFNetworking后,您只需:

if ([ImageArray count] >0){
    [recipeImageView setImageWithURL:[NSURL URLWithString:[ImageArray objectAtIndex:indexPath.row]] placeholderImage:nil];
}

希望你喜欢这个

我认为根本原因是加载下面两行图像的成本:

NSData *data0 = [NSData dataWithContentsOfURL: [NSURL URLWithString:[ImageArray objectAtIndex:indexPath.row]]];
UIImage *image = [UIImage imageWithData: data0];
我的解决方案是使用AFNetworking加载图像处理,请参考链接:

安装AFNetworking后,您只需:

if ([ImageArray count] >0){
    [recipeImageView setImageWithURL:[NSURL URLWithString:[ImageArray objectAtIndex:indexPath.row]] placeholderImage:nil];
}

希望你喜欢这个

隐藏这些行cell.layer.shouldRasterize=YES;cell.layer.rasterizationScale=[UIScreen mainScreen]。比例;并尝试隐藏这些行cell.layer.shouldRasterize=YES;cell.layer.rasterizationScale=[UIScreen mainScreen]。比例;再试一次