1、创建自定义相册并命名
- (PHAssetCollection * )createCollection{
// 创建一个新的相册,查看所有的自定义相册,先查看是否有自己要创建的自定义相册,如果没有自己要创建的自定义相册那么我们就进行创建
NSString * title = [NSBundle mainBundle].infoDictionary[(NSString *)kCFBundleNameKey];
PHFetchResult<PHAssetCollection *> *collections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
PHAssetCollection * createCollection = nil; // 最终要获取的自己创建的相册
for (PHAssetCollection * collection in collections) {
if ([collection.localizedTitle isEqualToString:title]) { // 如果有自己要创建的相册
createCollection = collection;
break;
}
}
if (createCollection == nil) { // 如果没有自己要创建的相册
// 创建自己要创建的相册
NSError * error1 = nil;
__block NSString * createCollectionID = nil;
// 这个方法会在相册创建完毕后才会返回(同步返回)
[[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
NSString * title = [NSBundle mainBundle].infoDictionary[(NSString *)kCFBundleNameKey];
createCollectionID = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:title].placeholderForCreatedAssetCollection.localIdentifier;
} error:&error1];
if (error1) {
NSLog(@"创建相册失败...");
}
// 创建相册之后我们还要获取此相册 因为我们要往进存储相片
createCollection = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[createCollectionID] options:nil].firstObject;
}
return createCollection;
}
2、保存图片至相册
- (void)saveImage:(UIImage *)image toCollection:(PHAssetCollection *)collection {
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
// 保存图片至相机胶卷 并拿到这个图片资源的标识
PHAssetChangeRequest *createAssetRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
PHObjectPlaceholder *assetPlaceholder = createAssetRequest.placeholderForCreatedAsset;
// 将“相机胶卷”中的图片添加到指定相册
PHAssetCollectionChangeRequest *albumChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:collection];
[albumChangeRequest addAssets:@[assetPlaceholder]];
} completionHandler:^(BOOL success, NSError * _Nullable error) {
if (error) {
NSLog(@"success = %d, error = %@", success, error);
}
}];
}
3、从指定相册读取照片资源
- (PHFetchResult *)getImagesFromCollection:(PHAssetCollection *)collection {
// 获取所有资源的集合,并按资源的创建时间逆序排序
PHFetchOptions *options = [[PHFetchOptions alloc] init];
// 根据照片创建时间来倒序排列
options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
PHFetchResult *result = [PHAsset fetchAssetsInAssetCollection:collection options:options];
return result;
}
4、删除指定相册的指定图片
- (void)removeAsset:(PHAsset *)asset fromCollection:(PHAssetCollection *)collection {
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
// 将指定相册的指定图片移除(这里只是把照片从相册中移除,照片依然存储在手机的相册胶卷中)
PHAssetCollectionChangeRequest *albumChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:collection];
[albumChangeRequest removeAssets:@[asset]];
// 将“相机胶卷”中的指定图片删除掉
[PHAssetChangeRequest deleteAssets:@[asset]];
} completionHandler:^(BOOL success, NSError * _Nullable error) {
if (error) {
NSLog(@"success = %d, error = %@", success, error);
}
}];
}
5、将相册中的照片资源转化为可用的image
PHFetchResult *result = [self getImagesFromCollection:_selfCollection];
[result enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
PHImageRequestOptions *option = [[PHImageRequestOptions alloc] init];
option.resizeMode = PHImageRequestOptionsResizeModeNone;
// 照片是否同步处理图像请求,设置为NO可能会多次调用resultHandle,设置为YES会阻塞调用线程直到图像数据准备好了或出现错误
option.synchronous = NO;
// targetSize设置为PHImageManagerMaximumSize,则image会返回原始尺寸
[[PHImageManager defaultManager] requestImageForAsset:(PHAsset *)obj targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeAspectFit options:option resultHandler:^(UIImage * _Nullable imgResult, NSDictionary * _Nullable info) {
// 是否原图
BOOL isOriginImg = ![[info objectForKey:PHImageCancelledKey] boolValue] && ![info objectForKey:PHImageErrorKey] && ![[info objectForKey:PHImageResultIsDegradedKey] boolValue];
if (isOriginImg) {
// do something you want to do with UIImage
}
}];
}];