gpt4 book ai didi

ios - 从 Document 目录加载多个图像时 UICollectionView 卡住

转载 作者:行者123 更新时间:2023-11-28 23:52:49 24 4
gpt4 key购买 nike

我正在加载超过 30-50 张图像,这些图像存储在文档目录中。它们所有的图像都立即加载到 collectionView 中,但是当滚动 collectionView 时它会卡住。

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"ImageCell";
ImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

//Load image from document directory
NSString *imageName = [arrCollection objectAtIndex:indexPath.row];
NSString *imagePath = [NSString stringWithFormat:@"%@/%@",(AppObj).imagefolderPath, imageName];
UIImage *localImg = [[UIImage alloc]initWithContentsOfFile:imagePath];

cell.imgSketch.image = localImg;

cell.txtName.text = [NSString stringWithFormat:@"%@",[imageName stringByReplacingOccurrencesOfString:@".png" withString:@""]];

cell.layer.masksToBounds = YES;
return cell;
}

我也试过下面的调度 block

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
UIImage *localImg = [[UIImage alloc]initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@",(AppObj).imagefolderPath,[arrCollection objectAtIndex:indexPath.row]]];
dispatch_async(dispatch_get_main_queue(), ^{
cell.imgSketch.image = localImg;
});
});

请建议我解决这个问题的任何解决方案。

提前致谢

最佳答案

您可以使用 SDWebImage它还显示文档目录中的图像。

[cell.imgSketch sd_setImageWithURL:[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@",(AppObj).imagefolderPath,[arrCollection objectAtIndex:indexPath.row]]]];

注意:使用fileURLWithPath当您创建 NSURL 的对象时,因为它是文件的路径。

关于ios - 从 Document 目录加载多个图像时 UICollectionView 卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51724416/

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