#pragma mark -- iOS 8.0 以上获取所有照片用Photos.h这个库
-(NSMutableArray *)getALLphotosUsingPohotKit
{
NSMutableArray *arr = [NSMutableArray array];
// 所有智能相册
PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
for (NSInteger i = 0; i < smartAlbums.count; i++) {
@autoreleasepool {
// 是否按创建时间排序
PHFetchOptions *option = [[PHFetchOptions alloc] init];
option.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
option.predicate = [NSPredicate predicateWithFormat:@"mediaType == %ld", PHAssetMediaTypeImage];
PHCollection *collection = smartAlbums[i];
//遍历获取相册
if ([collection isKindOfClass:[PHAssetCollection class]]) {
if ([collection.localizedTitle isEqualToString:@"所有照片"]) {
PHAssetCollection *assetCollection = (PHAssetCollection *)collection;
PHFetchResult *fetchResult = [PHAsset fetchAssetsInAssetCollection:assetCollection options:nil];
NSArray *assets;
if (fetchResult.count > 0) {
// 某个相册里面的所有PHAsset对象
assets = [self getAllPhotosAssetInAblumCollection:assetCollection ascending:YES ];
[arr addObjectsFromArray:assets];
}
}
}
}
}
//返回所有照片内的所有照片
return arr;
}
#pragma mark - < 获取相册里的所有图片的PHAsset对象 >
- (NSArray *)getAllPhotosAssetInAblumCollection:(PHAssetCollection *)assetCollection ascending:(BOOL)ascending
{
// 存放所有图片对象
NSMutableArray *assets = [NSMutableArray array];
// 是否按创建时间排序
PHFetchOptions *option = [[PHFetchOptions alloc] init];
option.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:ascending]];
option.predicate = [NSPredicate predicateWithFormat:@"mediaType == %ld", PHAssetMediaTypeImage];
// 获取所有图片对象
PHFetchResult *result = [PHAsset fetchAssetsInAssetCollection:assetCollection options:option];
// 遍历
[result enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL * _Nonnull stop) {
[assets addObject:asset];
}];
return assets;
}
调用方法:
其中,filename就是图片名字;格式:IMG_0776.JPG
网友评论