我正在使用YangMingShan作为照片选择器,这正是我需要的,但是当用户拥有大量照片时我遇到了问题。
我一直在测试手机上有25,000多张照片和250张专辑,当我展示视图控制器时,应用程序会在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];
}
答案 0 :(得分:0)
转到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];
}
}
}