1. 隐私申请权限
2. 同步获取低质量的图片
// 申请资源类型
PHAssetCollection *cameraRoll =[PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil].lastObject;
if (!cameraRoll) {
block(nil);
return;
}
PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
//设置为异步还是同步,默认是异步
options.synchronous = NO;
options.deliveryMode = PHImageRequestOptionsDeliveryModeFastFormat;
PHFetchResult *assets = [PHAsset fetchAssetsInAssetCollection:cameraRoll options:nil];
//创建缓存管理器
PHCachingImageManager *caching = [[PHCachingImageManager alloc] init];
for (PHAsset *asset in assets) {
if (asset.mediaType ==PHAssetMediaTypeImage) {
//获取低质量图片
[caching requestImageForAsset:asset targetSize:CGSizeMake(300, 300) contentMode:PHImageContentModeDefault options:options resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
if (result) {
NSLog(@"打印图片信息:%@", result);
}
}];
}
}
为什么获取获取低质量图片?
低质量图片获取速度快,即使相册里有两百张照片,获取出来也是不到2s
如果是高清原图,两百张至少得30s,这样体验不好,并且同步会卡死主线程
为什么用同步?
异步获取会出现各种问题,有时获取不全
3. 获取高清图片
PHAsset *asset =nil;
if (!<#self.asset#>) {
return;
}
asset =<#self.asset#>;
PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
options.resizeMode = PHImageRequestOptionsResizeModeNone;
//必须同步,否则有些照片获取不到.
options.synchronous = YES;
PHCachingImageManager *caching = [[PHCachingImageManager alloc] init];
//图片的原始尺寸
CGSize sizeOne = CGSizeMake(asset.pixelWidth, asset.pixelHeight);
[caching requestImageForAsset:asset targetSize:sizeOne contentMode:PHImageContentModeDefault options:options resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
if ([NSStringFromCGSize(result.size) isEqualToString:NSStringFromCGSize(sizeOne)]) {
NSLog(@"高清照片信息:%@", result);
}
}];
4. 删除某张照片
PHAsset *asset =nil;
if (!<#self.asset#>) {
return;
}
asset =<#self.asset#>;
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
[PHAssetChangeRequest deleteAssets:@[asset]];
} completionHandler:^(BOOL success, NSError * _Nullable error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error) {
NSLog(@"删除照片错误:%@", error);
}
});
}];
5. 创建相册/保存照片到某相册
/// 创建相册(该相册存在则返回相册对象,不存在则创建并返回)
+ (PHAssetCollection *)creactNewPhotosGroupWinthName:(NSString *)name {
NSString *title = name;
// 获得所有的自定义相册
PHFetchResult *collections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
for (PHAssetCollection *collection in collections) {
if ([collection.localizedTitle isEqualToString:title]) {
return collection;
}
}
// 代码执行到这里,说明还没有自定义相册
__block NSString *createdCollectionId = nil;
// 创建一个新的相册
[[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
createdCollectionId = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:title].placeholderForCreatedAssetCollection.localIdentifier;
} error:nil];
if (createdCollectionId == nil) return nil;
// 创建完毕后再取出相册
return [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[createdCollectionId] options:nil].firstObject;
}
/// 保存图片到指定相册
+ (void)saveImage:(UIImage *)image toCollection:(PHAssetCollection*)collection completionBlock:(void(^)(BOOL success, NSError * error))block {
if (!image || ![image isKindOfClass:[UIImage class]]) {
block(NO, [NSError errorWithDomain:@"图片格式不正确" code:0 userInfo:nil]);
return;
}
__block NSString *assetId = nil;
// 1. 存储图片到"相机胶卷"
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetChangeRequest *request =[PHAssetCreationRequest creationRequestForAssetFromImage:image];
// 2. 先保存图片到"相机胶卷",拿到标识符
assetId = request.placeholderForCreatedAsset.localIdentifier;
// request.location =[[CLLocation alloc]initWithLatitude:30.000 longitude:14.000];
} completionHandler:^(BOOL success, NSError * _Nullable error) {
if (error) {
NSLog(@"保存到默认相册失败:%@", error);
block(success, error);
return;
}
//如果没指定相册对象,就不再处理
if (!collection) {
block(success, error);
return;
}
// 3. 将“相机胶卷”中的图片添加到新的相册
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetCollectionChangeRequest *request = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:collection];
// 根据唯一标示获得相片对象
PHAsset *asset = [PHAsset fetchAssetsWithLocalIdentifiers:@[assetId] options:nil].firstObject;
if (asset) {
// 添加图片到相册中
[request addAssets:@[asset]];
}
} completionHandler:^(BOOL success, NSError * _Nullable error) {
block(success, error);
if (error) {
NSLog(@"保存到指定相册失败:%@", error);
}
}];
}];
}