ios8之后, 获取相册就开始用PhotoKit
经过几波爬坑, 基本优化的很多
- (void)getAllAblumListFolderWithComplete:(void(^)(NSMutableArray * result))complate
{
NSMutableArray<JYAblumList *> *photoAblumList = [NSMutableArray array];
_photoFoldersArray = photoAblumList;
// dispatch_async(dispatch_get_global_queue(0, 0), ^{
//获取所有智能相册
PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAny options:nil];
if (smartAlbums.count != 0) {
[smartAlbums enumerateObjectsUsingBlock:^(PHAssetCollection * _Nonnull collection, NSUInteger idx, BOOL *stop) {
//过滤掉视频和最近删除
if (![collection.localizedTitle isEqualToString:@"Recently Deleted"]) {
PHFetchResult* result = [PHAsset fetchKeyAssetsInAssetCollection:collection options:nil];
if (result.firstObject) {
JYAblumList *ablum = [[JYAblumList alloc] init];
ablum.title = collection.localizedTitle;
ablum.count = result.count;
ablum.headImageAsset = result.firstObject;
ablum.assetCollection = collection;
[photoAblumList addObject:ablum];
}
}
if (idx == smartAlbums.count - 1) {
complate(photoAblumList);
}
}];
}
//获取用户创建的相册
PHFetchOptions *options = [[PHFetchOptions alloc] init];
options.predicate = [NSPredicate predicateWithFormat:@"estimatedAssetCount!= 0"];
PHFetchResult *userAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:options];
if (userAlbums.count != 0) {
[userAlbums enumerateObjectsUsingBlock:^(PHAssetCollection * _Nonnull collection, NSUInteger idx, BOOL * _Nonnull stop) {
PHFetchResult* result = [PHAsset fetchKeyAssetsInAssetCollection:collection options:nil];
if (result.firstObject) {
JYAblumList *ablum = [[JYAblumList alloc] init];
ablum.title = collection.localizedTitle;
ablum.count = result.count;
ablum.headImageAsset = result.firstObject;
ablum.assetCollection = collection;
[photoAblumList addObject:ablum];
}
if (idx == userAlbums.count - 1) {
complate(photoAblumList);
}
}];
}
}
划重点:
PHFetchOptions options = [[PHFetchOptions alloc] init];
options.predicate = [NSPredicate predicateWithFormat:@"estimatedAssetCount!= 0"];
这个estimatedAssetCount 在系统相册返回的值是不准确的, 在获取用户自定义相册的数量是准确的,我只是用来去掉用户自定义的空文件夹,优化整个遍历流程.
获取系统相册的准确数值
应该用
PHFetchResult result = [PHAsset fetchKeyAssetsInAssetCollection:collection options:nil];
result.count就是相册里面照片的数量
然后两个block触发事件就是会刷新两次事件,目前对我来说只能这样
接下来就是获取相应的相册的图片
/**
按时间分照片
@param assetCollection PHAssetCollection
@param ascending BOOL
@param complate block
*/
-(void)getAllPhotoVideoAssetDateCollection:(PHAssetCollection*) assetCollection ascending:(BOOL)ascending andComplete:(void(^)(NSMutableArray * result))complate{
PHFetchResult *result = [self fetchAssetsInAssetCollection:assetCollection ascending:ascending];
NSMutableArray* dateStringArray = [NSMutableArray array];
NSMutableArray* dateModelArray = [NSMutableArray array];
[result enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
PHAsset* asset = obj;
if (asset.mediaType == PHAssetMediaTypeImage) {
NSString* createDateString = [NSString stringImageDate:asset.creationDate];
JSImageModel* model = [[JSImageModel alloc] init];
model.photoAsset = asset;
model.photoName = [[asset.localIdentifier stringByReplacingOccurrencesOfString:@"/" withString:@"__"] stringByAppendingString:@".jpg"];
model.fileName = model.photoName;
model.shortenFileName = model.fileName;
model.myType = JSUploadTypePhotoAlbum;
[self addDateModelWithDateString:createDateString toModelArray:dateModelArray withDateStringArray:dateStringArray andModel:model];
}else if (asset.mediaType == PHAssetMediaTypeVideo){
NSString* createDateString = [NSString stringImageDate:asset.creationDate];
JSImageModel* model = [[JSImageModel alloc] init];
PHAsset* asset = obj;
model.photoAsset = asset;
model.photoName = [[asset.localIdentifier stringByReplacingOccurrencesOfString:@"/" withString:@"__"] stringByAppendingString:@"AMovie.mp4"];
model.fileName = model.photoName;
model.shortenFileName = model.fileName;
model.myType = JSUploadTypeVideoAlbum;
[self addDateModelWithDateString:createDateString toModelArray:dateModelArray withDateStringArray:dateStringArray andModel:model];
}
if (idx == result.count - 1) {
complate(dateModelArray);
}
}];
}