I am using PhotoKit
to fetch photos from system album and put them in an UICollectionView
.
For the
UICollectionViewCell
, I set it like this:cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
When initialing my
UICollectionView
, I fetch the photoPHAsset
fromcollection
:Camera Roll
only: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]; } }];
Then I request the
UIImage
fromPHFetchResult
from above like this:-(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; }
But when I run it and scroll the UICollectionView
fast enough, I found the memory use becomes steep like this:
How can I reduce it in case it would crash when memory is not enough?