UICollectionView加载本地图片滚动卡顿解决

问题:如果只传递图片路径给UICollectionViewCell,在每次加载Cell时候都要通过imageWithContentsOfFile:读取一次本地图片,而该方法并没有图片缓存,从而滚动不断读取本地图片加载到Cell造成卡顿.

解决:

1.定义成员属性

/** 图片提前缓存(直接传递Image给cell,避免cell不断读取本地数据卡顿imageWithContentsOfFile) */
@property (nonatomic, strong) NSMutableArray *images;

2.懒加载

#pragma mark - 懒加载读取本地图片
- (NSMutableArray *)images {
    if (!_images) {
        _images = [NSMutableArray array];
        for (NSInteger i=0; i

3.直接传递UIImage对象给Cell

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ZLZLLookChartViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    cell.image = self.images[indexPath.item];

    return cell;
}

你可能感兴趣的:(UICollectionView加载本地图片滚动卡顿解决)