gpt4 book ai didi

ios - 将文档目录中的图像异步加载到 UICollectionView

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:26:04 24 4
gpt4 key购买 nike

我正在尝试将文档目录中的图像加载到 UICollectionView 中。它的工作原理:流畅,快速....(笑)。但我遇到一个问题:当我调用 [collectionView reloadData] 时,图像闪烁(一次)。这是我的代码:

- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView_ cellForItemAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIdentifier = NSStringFromClass([AFMyRecentCollectionViewCell class]);
AFMyRecentCollectionViewCell *cell = (AFMyRecentCollectionViewCell*)[collectionView_ dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
AFMyRecentObject *recent = [[AFRecentManager sharedInstance].arrayRecent objectAtIndex:indexPath.row];

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
UIImage *image;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *filePath = [self databasePathWithPathComponent:[NSString stringWithFormat:@"%@.jpg", recent.id_film]];
if ([fileManager fileExistsAtPath:filePath] == YES) {
image = [UIImage imageWithContentsOfFile:filePath];
}

dispatch_async(dispatch_get_main_queue(), ^{
if (image) {
cell.imageView.image = image;
} else {
cell.imageView.image = [UIImage imageNamed:@"loadding.png"];
}
});
});

cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;

return cell;
}

- (NSString *)databasePathWithPathComponent:(NSString*)pathComponent {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex: 0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:pathComponent];
return path;
}

最佳答案

您必须放置默认/占位符图像以防止出现这种情况..

    // this prevents the flicker/blinks
cell.imageView.image = nil; // if you dont have any image as placeholder

//since you have
cell.imageView.image = [UIImage imageNamed:@"loadding.png"]; // this i assume your placeholder image?

dispatch_async(queue, ^{
//your codes..
dispatch_async(dispatch_get_main_queue(), ^{
// your codes..
});
});

关于ios - 将文档目录中的图像异步加载到 UICollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30472590/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com