ios - 通过 PHFetchResult 获取照片非常慢,有很多照片

标签 ios objective-c

我正在使用 YangMingShan作为照片选择器,这正是我所需要的,但是当用户有很多照片时我遇到了问题。

我一直在使用包含 25,000 多张照片和 250 个相册的手机进行测试,当我展示 View Controller 时,应用程序在 collectionView 加载之前卡住了大约 30 秒。

这是有问题的代码,我试图弄清楚是否有更优化的方法来获取结果。

- (void)fetchCollections
{

NSMutableArray *allAblums = [NSMutableArray array];

    PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];

    __block __weak void (^weakFetchAlbums)(PHFetchResult *collections);
    void (^fetchAlbums)(PHFetchResult *collections);
    weakFetchAlbums = fetchAlbums = ^void(PHFetchResult *collections) {
        // create fecth options
        PHFetchOptions *options = [PHFetchOptions new];
        options.predicate = [NSPredicate predicateWithFormat:@"mediaType = %d",PHAssetMediaTypeImage];
        options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];

        for (PHCollection *collection in collections) {
            if ([collection isKindOfClass:[PHAssetCollection class]]) {
                PHAssetCollection *assetCollection = (PHAssetCollection *)collection;
                PHFetchResult *assetsFetchResult = [PHAsset fetchAssetsInAssetCollection:assetCollection options:options];
                if (assetsFetchResult.count > 0) {
                    [allAblums addObject:@{@"collection": assetCollection
                                           , @"assets": assetsFetchResult}];
                }
            }
            else if ([collection isKindOfClass:[PHCollectionList class]]) {
                // If there are more sub-folders, dig into the collection to fetch the albums
                PHCollectionList *collectionList = (PHCollectionList *)collection;
                PHFetchResult *fetchResult = [PHCollectionList fetchCollectionsInCollectionList:(PHCollectionList *)collectionList options:nil];
                weakFetchAlbums(fetchResult);
            }
        }
    };

    PHFetchResult *topLevelUserCollections = [PHCollectionList fetchTopLevelUserCollectionsWithOptions:nil];
    fetchAlbums(topLevelUserCollections);

    for (PHAssetCollection *collection in smartAlbums) {
        PHFetchOptions *options = [PHFetchOptions new];
        options.predicate = [NSPredicate predicateWithFormat:@"mediaType = %d",PHAssetMediaTypeImage];
        options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
        PHFetchResult *assetsFetchResult = [PHAsset fetchAssetsInAssetCollection:collection options:options];
        if (assetsFetchResult.count > 0) {

            // put the "all photos" in the first index
            if (collection.assetCollectionSubtype == PHAssetCollectionSubtypeSmartAlbumUserLibrary) {
                [allAblums insertObject:@{@"collection": collection
                                              , @"assets": assetsFetchResult} atIndex:0];
            }
            else {
                [allAblums addObject:@{@"collection": collection
                                           , @"assets": assetsFetchResult}];
            }
        }
    }
    self.collectionItems = [allAblums copy];
}

最佳答案

转到 YMSPhotoPickerViewController.m 并更改功能。

- (void)refreshPhotoSelection
{
  if ([self shouldOrderSelection]) {

    PHFetchResult *fetchResult = self.currentCollectionItem[@"assets"];

    for (NSInteger i=0; i<self.selectedPhotos.count; i++) {
      PHAsset *needReloadAsset = self.selectedPhotos[i];
      YMSPhotoCell *cell = (YMSPhotoCell *)[self.photoCollectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:[fetchResult indexOfObject:needReloadAsset]+1 inSection:0]];
      cell.selectionOrder = i+1;
      [self.photoCollectionView selectItemAtIndexPath:[NSIndexPath indexPathForItem:[fetchResult indexOfObject:needReloadAsset]+1 inSection:0] animated:NO scrollPosition:UICollectionViewScrollPositionNone];
    }
  }
}

关于ios - 通过 PHFetchResult 获取照片非常慢,有很多照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45746973/

相关文章:

ios - Cocoapods 与 github fork 项目

objective-c - 在 Cocoa Touch 中字符串包含 ' 时 NSPredicate 错误

ios - 如何在 React Native 中实现单选按钮

ios - 静态链接非图片第三方库

ios - 带有圆形边框的 UIImage,就像在联系人中一样

objective-c - 在 Obj-C 中使用用户名定义全局常量

objective-c - 如何在 Objective-C 中实例化一个不从 NSObject 继承的类

objective-c - 从 OS X 应用程序挂载网络卷

iOS系统图标

iOS Callkit 和后台模式