【发布时间】:2015-09-07 03:06:35
【问题描述】:
我正在使用PhotoKit 从系统相册中获取照片并将它们放入UICollectionView。
-
对于
UICollectionViewCell,我是这样设置的:cell.imageView.contentMode = UIViewContentModeScaleAspectFill; -
初始化我的
UICollectionView时,我从collection获取照片PHAsset:仅限Camera Roll:PHFetchResult *fetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil]; [fetchResult enumerateObjectsUsingBlock:^(PHAssetCollection *collection, NSUInteger idx, BOOL *stop) { NSLog(@"ALBUM NAME: %@", collection.localizedTitle); if ([collection.localizedTitle isEqualToString:@"Camera Roll"]) { _fetchedPhotos = [PHAsset fetchAssetsInAssetCollection:collection options:nil]; _pickedStatuses = @[].mutableCopy; NSMutableArray *assets = @[].mutableCopy; _manager = [[PHCachingImageManager alloc] init]; _options = [[PHImageRequestOptions alloc] init]; _options.resizeMode = PHImageRequestOptionsResizeModeExact; _options.deliveryMode = PHImageRequestOptionsDeliveryModeOpportunistic; CGFloat scale = [UIScreen mainScreen].scale; CGSize targetSize = CGSizeMake(layout.itemSize.width*scale, layout.itemSize.height*scale); //I'm not sure if this api should be called here [_manager startCachingImagesForAssets:assets targetSize:targetSize contentMode:PHImageContentModeAspectFill options:_options]; } }]; -
然后我从上面的
PHFetchResult请求UIImage,如下所示:-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { MYCell *cell = (MYCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"reuseCell" forIndexPath:indexPath]; CGFloat scale = [UIScreen mainScreen].scale; CGSize targetSize = CGSizeMake(_layout.itemSize.width*scale, _layout.itemSize.height*scale); PHAsset *asset = _fetchedPhotos[indexPath.item]; [_manager requestImageForAsset:asset targetSize:targetSize contentMode:PHImageContentModeAspectFill options:_options resultHandler:^(UIImage *result, NSDictionary *info) { cell.imageView.image = result; }]; return cell; }
但是当我运行它并足够快地滚动UICollectionView时,我发现内存使用变得像这样急剧:
如果内存不足会崩溃,如何减少它?
【问题讨论】:
-
我期待这种模式,因为在 PhotoKit 可以创建缩略图之前,它必须将整个图像加载到内存中。另外,您是在设备上还是在模拟器中进行测试?两者之间的内存行为会有很大不同,因此请始终在设备上进行测试,以了解您的应用性能的真实情况。
-
我总是在我的 iPhone 6 Plus 设备上测试它。
标签: ios objective-c photokit