iOS之Photos/Photos.h库获取系统相册

在iOS11中使用了系统photos库获取本地相册,可以先获取相册名再获取具体照片,同时可以访问系统分类相册和用户自定义的相册名。

#pragma mark - 获取本地用户新建相册 这里只获取相册名
- (void)getLocalImageAlbumSuccess:(fileSuccessBlock)success failed:(failedBlock)failed{
    //相册视频
    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
    if (status == PHAuthorizationStatusRestricted || status == PHAuthorizationStatusDenied) {
        // 这里便是无访问权限
   
    }  else {
        [SVProgressHUD show];
            dispatch_async(dispatch_get_global_queue(0, 0), ^{
            NSMutableArray *albums = [NSMutableArray array];
            // 个人创建相册
            // PHFetchResult *userAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil];
            // 所有智能相册
                NSArray *titles = @[@"最近删除",@"视频",@"慢动作",@"动图",@"延时摄影"];
                PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
                [smartAlbums enumerateObjectsUsingBlock:^(PHAssetCollection * _Nonnull collection, NSUInteger idx, BOOL * _Nonnull stop) {
                    if ([collection isKindOfClass:[PHAssetCollection class]]) {
                        PHFetchResult *fetchResult = [PHAsset fetchAssetsInAssetCollection:collection options:nil];
                        if (fetchResult.count > 0) {
                            if (![titles containsObject:collection.localizedTitle]) {
                                [albums addObject:collection];
                            }
                        }
                    }
                }];
            if (albums.count > 0) {
                success(albums);
            }
        });
    }
}

#pragma mark - 根据PHAssetCollection获取具体照片
- (void)getLocalAlbumImageListWithCollection:(PHAssetCollection *)collection successInfos:(fileSuccessBlock)successInfos successImages:(fileSuccessBlock)successImages failed:(fileFailedBlock)failed{
    [SVProgressHUD show];
            dispatch_async(dispatch_get_global_queue(0, 0), ^{
                {
                    NSMutableArray *imageInfos = [NSMutableArray array];
                    NSMutableArray *images = [NSMutableArray array];
                    
                    PHFetchResult *fetchResult = [PHAsset fetchAssetsInAssetCollection:collection options:nil];
                    if (fetchResult.count != 0) {
                            [fetchResult enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL * _Nonnull stop) {
                                
                                [imageInfos addObject:asset];
                                
                                //从相册中取出照片
                                PHImageRequestOptions *opt = [[PHImageRequestOptions alloc]init];
                                opt.resizeMode = PHImageRequestOptionsResizeModeFast;//缩率图
                                opt.synchronous = YES;
                                PHImageManager *imageManager = [[PHImageManager alloc] init];
                                [imageManager requestImageForAsset:asset targetSize:CGSizeMake(100, 100) contentMode:PHImageContentModeAspectFill options:opt resultHandler:^(UIImage * _Nullable image, NSDictionary * _Nullable info) {
                                    [images addObject:image];
                                }];
                                
                                if (images.count > 400) {
                                    //因为部分手机照片过多,会引起系统内存奔溃,所有限制了获取数量
                                    FF_DLog(@"images > 100");
                                    *stop = YES;
                                }
                            }];
                        
                        [SVProgressHUD dismiss];
                        if (images.count > 0) {
                            successImages(images);
                        }
                        if (imageInfos.count > 0) {
                            successInfos(imageInfos);
                        }
                    }
                    
                }
            });
}

你可能感兴趣的:(iOS之Photos/Photos.h库获取系统相册)