我正在使用UICollectionView并且当前拥有全屏单元格,当我滑动到下一个单元格时,图像需要加载到单元格中(因为它们是高分辨率)。我已经使用PHCachingImageManager来预先缓存图像,并且在我加载单元格之前它已经被缓存了,但是只是将图像加载到imageView中会花费明显的时间。我想知道是否有一种方法可以预先加载隐藏的下一个单元格,然后才能进入cellForItemAtIndexPath?
有什么想法吗?
答案 0 :(得分:1)
显然需要一些时间来加载已经从PHCachingImageManager缓存的图像,这是我延迟的原因。我创建了一个测试函数,在设置当前单元格时保存了下一个图像(并且还会添加前一个图像),并且我的闪烁不再存在。当我清理它时,我会发布工作示例......
<强>更新强> 所以我创建了一个_images NSMutableDictionary来存储已经加载的图像并使用这个函数填充它
- (void)startCachingSurroundingImages:(NSInteger)index {
CGSize targetSize = PHImageManagerMaximumSize;
PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
[options setNetworkAccessAllowed:YES];
[options setDeliveryMode:PHImageRequestOptionsDeliveryModeHighQualityFormat];
if (index < _imagesArray.count - 1 && ![_images objectForKey:[NSIndexPath indexPathForItem:0 inSection:index+1]]) {
NSDictionary *assetDictionary = [_imagesArray objectAtIndex:index + 1];
NSString *assetRefID = [assetDictionary objectForKey:@"asset_id"];
PHFetchResult *assetFetchResult = [PHAsset fetchAssetsWithLocalIdentifiers:@[assetRefID] options:[PHFetchOptions new]];
PHAsset *asset = [assetFetchResult firstObject];
[photoManager requestImageForAsset:asset targetSize:targetSize contentMode:PHImageContentModeAspectFit options:options resultHandler:^(UIImage *result, NSDictionary *info) {
if (result) {
[_images setObject:result forKey:[NSIndexPath indexPathForItem:0 inSection:index+1]];
}
}];
}
if (index - 1 >= 0 && ![_images objectForKey:[NSIndexPath indexPathForItem:0 inSection:index-1]]) {
NSDictionary *leftAssetDictionary = [_imagesArray objectAtIndex:index - 1];
NSString *leftAssetRefID = [leftAssetDictionary objectForKey:@"asset_id"];
PHFetchResult *leftAssetFetchResult = [PHAsset fetchAssetsWithLocalIdentifiers:@[leftAssetRefID] options:[PHFetchOptions new]];
PHAsset *leftAsset = [leftAssetFetchResult firstObject];
[photoManager requestImageForAsset:leftAsset targetSize:targetSize contentMode:PHImageContentModeAspectFit options:options resultHandler:^(UIImage *result, NSDictionary *info) {
if (result) {
[_images setObject:result forKey:[NSIndexPath indexPathForItem:0 inSection:index-1]];
}
}];
}
}
我在cellForItemAtIndexPath中调用它就像这样......
[self startCachingSurroundingImages:indexPath.section];
仍然在cellForItemAtIndexPath中,我像这样加载图像...
if ([_images objectForKey:indexPath]) {
cell.imageView.image = [_images objectForKey:indexPath];
[photoCell.activityIndicator setHidden:YES];
} else {
photoCell.tag = (int)[photoManager requestImageForAsset:asset targetSize:targetSize contentMode:PHImageContentModeAspectFit options:options resultHandler:^(UIImage *result, NSDictionary *info) {
PhotoCell *photoCell = (PhotoCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
if (photoCell && result) {
photoCell.imageView.image = result;
[photoCell.activityIndicator setHidden:YES];
} else {
NSLog(@"BAD IMAGE!!! %@", info);
[photoCell.activityIndicator setHidden:NO];
}
}];
}
然后在didReceiveMemoryWarning中我清理了一下......
- (void)didReceiveMemoryWarning {
NSArray *allKeys = [_images allKeys];
for (int i = 0; i < _images.count; i++) {
NSIndexPath *path = [allKeys objectAtIndex:i];
if (path.section != _lastIndex && path.section != _lastIndex - 1 && path.section != _lastIndex + 1) {
[_images removeObjectForKey:path];
}
}
// Though one could just as easily do this
// [_images removeAllObjects];
}
它不漂亮,但它确实有效。