重新加载和调用 requestImageForAsset 时 UICollectionView 闪烁

UICollectionView Flicker When Reloading And Calling requestImageForAsset

我有一个 UICollectionView 的问题,我想我已经缩小到以下区域。问题是,当我在 UICollectionView 上调用重新加载时,会出现闪烁,加载 1 张图像,然后加载正确的图像。几乎就像您滚动新单元格并更新它们但没有任何滚动一样。

现在我用普通的

测试了它
sampleImage.image = [UIImage imageNamed:@"LogoHome"];

而且我没有任何闪烁,所以我将其缩小到以下代码

[self.imageManager requestImageForAsset:[self.arrayPhotoAssets objectAtIndex:indexPath.row]
                                 targetSize:CGSizeMake(imageWidth, imageHeight)
                                contentMode:PHImageContentModeAspectFill
                                    options:nil resultHandler:^(UIImage *result, NSDictionary* info){

                                        UIImageView *sampleImage = (UIImageView *)[cell viewWithTag:1];
                                        sampleImage.image = result;

                                    }];

里面
- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 

有什么方法可以调整代码以防止上述情况发生?这是完整的片段:

- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

////// GALLERY COLLECTION VIEW


if(collectionView.tag == 1){

    static NSString *cellIdentifier = @"galleryCell";

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

    float imageWidth = 77;
    float imageHeight = 77;

    UIScreen *mainScreen = [UIScreen mainScreen];

    //NSLog(@"%f",mainScreen.bounds.size.width);

    // IPHONE 6

    if(mainScreen.bounds.size.width == 375){

        imageWidth = 92;
        imageHeight = 92;

    }

    // IPHONE 6 PLUS

    if(mainScreen.bounds.size.width == 414){

        imageWidth = 102;
        imageHeight = 102;

    }


    [self.imageManager requestImageForAsset:[self.arrayPhotoAssets objectAtIndex:indexPath.row]
                                 targetSize:CGSizeMake(imageWidth, imageHeight)
                                contentMode:PHImageContentModeAspectFill
                                    options:nil resultHandler:^(UIImage *result, NSDictionary* info){

                                        UIImageView *sampleImage = (UIImageView *)[cell viewWithTag:1];
                                        sampleImage.image = result;

                                    }];

    return cell;

}


UICollectionViewCell *cell;

return cell;

}

我建议您先设置 sampleImage.image = nil,然后再尝试调用 requestImageForAsset。这将清除您的图像视图,从而消除闪烁。

UIImageView *sampleImage = (UIImageView *)[cell viewWithTag:1];
sampleImage.image = nil;

[self.imageManager requestImageForAsset:[self.arrayPhotoAssets objectAtIndex:indexPath.row]
                                 targetSize:CGSizeMake(imageWidth, imageHeight)
                                contentMode:PHImageContentModeAspectFill
                                    options:nil resultHandler:^(UIImage *result, NSDictionary* info){

                                        sampleImage.image = result;
                             }];

在传递给 requestImage (PHImageRequestOptions) 的选项中将 "isSynchronous" 设置为 true 为我解决了闪烁问题。