gpt4 book ai didi

ios - 如何减少 PhotoKit 的内存使用?

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:57:48 34 4
gpt4 key购买 nike

我正在使用 PhotoKit 从系统相册中获取照片并将它们放入 UICollectionView 中。

  1. 对于 UICollectionViewCell,我这样设置:

    cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
  2. 初始化我的 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];
    }
    }];
  3. 然后我从上面的 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 时,我发现内存使用变得像这样陡峭:

memory use

如果内存不足会崩溃,如何减少它?

最佳答案

我现在做的是

  1. 减少获取的PHAsset对象的目标大小;
  2. 尝试采用缓存。我的代码如下:

    CGFloat scale = 1.5; //or an even smaller one
    if (!_manager) {
    _manager = [[PHCachingImageManager alloc] init];
    }
    if (!_options) {
    _options = [[PHImageRequestOptions alloc] init];
    }
    _options.resizeMode = PHImageRequestOptionsResizeModeExact;
    _options.deliveryMode = PHImageRequestOptionsDeliveryModeOpportunistic;

    NSRange range = NSMakeRange(0, _fetchedPhotos.count);
    NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:range];
    NSArray *assets = [_fetchedPhotos objectsAtIndexes:set];

    CGSize targetSize = CGSizeMake(_layout.itemSize.width*scale, _layout.itemSize.height*scale);

    [_manager startCachingImagesForAssets:assets targetSize:targetSize contentMode:PHImageContentModeAspectFill options:_options];

现在,即使我足够快地滚动 UICollectionView,顶部内存也不会超过 50 MB,这要归功于 Caching(我猜它基于我的代码工作)它没有那么大的波动,内存使用是这样的:

better memory use

更新根据另一篇文章 here , 建议不要指定 PHImageRequestOptions 对象。相反,您可以将它留给 iOS 来决定什么是最适合您的,以最好的质量和最短的时间呈现照片。

关于ios - 如何减少 PhotoKit 的内存使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32430531/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com