【发布时间】:2018-08-07 10:21:58
【问题描述】:
我正在加载超过 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;
}
我也尝试了以下调度块
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,它非常易于使用,并且以异步方式加载和缓存图像。 github.com/rs/SDWebImage
-
@vivekDas 我认为当我们想从 Web URL 加载图像时使用 SDWebImage
-
您可以尝试使用 imagePath 而不是 web url 来检查它是否有效。
-
@vivekDas
[cell.imgSketch sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",(AppObj).imagefolderPath,[arrCollection objectAtIndex:indexPath.row]]]];这个我试过了,但是不行
标签: ios objective-c asynchronous uicollectionview