gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:23:34 31 4
gpt4 key购买 nike

我正在使用 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/

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