PHAsset获取相册资源

[self getAllAssetInPhotoAblumWithAscending:YES];

#pragma mark - 获取相册内所有照片资源
- (NSArray *)getAllAssetInPhotoAblumWithAscending:(BOOL)ascending
{

    _pictureArr = [[NSMutableArray alloc]init];

    PHFetchOptions *option = [[PHFetchOptions alloc] init];
    //ascending 为YES时,按照照片的创建时间升序排列;为NO时,则降序排列
    option.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:ascending]];
    PHFetchResult *result = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:option];
    [result enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        PHAsset *asset = (PHAsset *)obj;

        if ([obj isKindOfClass:[PHAsset class]]) {
             [_pictureArr addObject:asset];
        }
    }];

    PHCachingImageManager *cachingImage = [[PHCachingImageManager alloc] init];
    [cachingImage startCachingImagesForAssets:_pictureArr targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeAspectFit options:nil];
    return _pictureArr;
}


    PHAsset *asset = _pictureArr[indexPath.row];
    PHImageRequestOptions *option = [[PHImageRequestOptions alloc] init];
    option.resizeMode = PHImageRequestOptionsResizeModeExact;
    option.networkAccessAllowed = YES;
        [[PHCachingImageManager defaultManager] requestImageForAsset:asset targetSize:PHImageManagerMaximumSize/*自定义获取图片的大小*/ contentMode:PHImageContentModeAspectFit options:option resultHandler:^(UIImage * _Nullable image, NSDictionary * _Nullable info) {
            //解析出来的图片
            [_valueMutableArr addObject:image];
        NSURL *url = [info objectForKey:@"PHImageFileURLKey"];
         //只有在不裁剪的情况下才能取出URL
        }]

再上传图片时,如果直接取uiimage上传时内存升高

  [[PHImageManager defaultManager]requestImageDataForAsset:asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
  //上传 NSData * _Nullable imageData
}];

你可能感兴趣的:(PHAsset,照片多选,相册)