iOS Photos的使用

1、获取所有照片


// 获取所有照片相册

PHFetchResult * result = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:nil];

PHImageRequestOptions * options = [[PHImageRequestOptions alloc] init];

options.resizeMode = PHImageRequestOptionsResizeModeFast;

options.synchronous = NO;

options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;

// 照片大小

CGFloat scale = [UIScreen mainScreen].scale;

CGFloat size = SCREEN_WIDTH * scale;

// 遍历所有照片

for (PHAsset *asset in assets) {

[[PHImageManager defaultManager] requestImageForAsset:asset targetSize:CGSizeMake(size, size) contentMode:PHImageContentModeAspectFit options:options

resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {

UIImage * photo = [UIImage imageWithData:UIImageJPEGRepresentation(result, 0.9)];

});

}];

}


2、获取用户相册


PHFetchOptions * options = [[PHFetchOptions alloc] init];

PHFetchResult * albums = [PHAssetCollection fetchTopLevelUserCollectionsWithOptions:options]; 

// 设置相片

CGFloat scale = [UIScreen mainScreen].scale;

CGFloat size = SCREEN_WIDTH * scale;

PHImageRequestOptions * options = [[PHImageRequestOptions alloc] init];

options.resizeMode = PHImageRequestOptionsResizeModeFast;

options.synchronous = NO;

options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;

// 遍历相册

[albums enumerateObjectsUsingBlock:^(PHAssetCollection * _Nonnull collection, NSUInteger idx, BOOL *stop) { 

// 相册名称 

NSString * title = collection.localizedTitle;          

// 相册里面的照片

PHFetchResult * assets = [PHAsset fetchAssetsInAssetCollection:collection options:nil];

// 遍历照片

for (PHAsset *asset in assets) {

[[PHImageManager defaultManager] requestImageForAsset:asset targetSize:CGSizeMake(size, size)

contentMode:PHImageContentModeAspectFit options:option

resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {

UIImage * photo = [UIImage imageWithData:UIImageJPEGRepresentation(result, 0.9)];

}];

}

}];

}

你可能感兴趣的:(iOS Photos的使用)