问题:如果只传递图片路径给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<self.datas.count; i++) {
// 图片名称
NSString *imageName = [_datas[i] objectForKey:kImageNameAndKey];
// 读取本地图片
UIImage *image = [UIImage imageWithContentsOfFile:kLocalImagePath(imageName)];
// 添加到图片存储数组
[_images addObject:image];
}
}
return _images;
}
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;
}