在ios8以下的系统创建新相册,然后在相册中手动删除了这个"琳小兮"相册,再使用下面的代码重新创建"琳小兮"相册是没有问题的:
[assetsLibrary addAssetsGroupAlbumWithName:@"琳小兮" resultBlock:^(ALAssetsGroup *group) { [groups addObject:group]; } failureBlock:nil];
ios8+ 包含ios8系统,在创建的时候用上面的代码则会崩溃,需要额外添加Photos.framework框架,并导入头文件#import <Photos/Photos.h>,主要修改的代码部分是把上面那段代码做如下修改:
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^ { [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:@"琳小兮"]; } completionHandler:^(BOOL success, NSError *error) { if (!success) { NSLog(@"---error:%@",error.userInfo); } }];
下面是完整的demo,结合了网上的实例写的,给大家做一个参考:
1.添加AssetsLibrary.framework 和 Photos.framework框架
2.导入头文件
#import <AssetsLibrary/AssetsLibrary.h>
#import <Photos/Photos.h>
3.创建全局 UIImageView *_imageV;
- (void)viewDidLoad { [super viewDidLoad]; _imageV = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; _imageV.image = [UIImage imageNamed:@"name.png"]; [self.view addSubview:_imageV]; [self createAlbumInPhoneAlbum]; } #pragma mark - 在手机相册中创建相册 - (void)createAlbumInPhoneAlbum { ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init]; NSMutableArray *groups=[[NSMutableArray alloc]init]; ALAssetsLibraryGroupsEnumerationResultsBlock listGroupBlock = ^(ALAssetsGroup *group, BOOL *stop) { if (group) { [groups addObject:group]; } else { BOOL haveHDRGroup = NO; for (ALAssetsGroup *gp in groups) { NSString *name =[gp valueForProperty:ALAssetsGroupPropertyName]; if ([name isEqualToString:@"琳小兮"]) { haveHDRGroup = YES; } } if (!haveHDRGroup) { //添加一个新的相册 //此方法在ios8.0系统上,之前用代码创建的相册,手动删除它,再用这个库去创建刚被删除的相册,这个时候方法回调是不会跑fail模块的,它走的是正常流程,但是带回来的group却是nil // [assetsLibrary addAssetsGroupAlbumWithName:@"琳小兮" // resultBlock:^(ALAssetsGroup *group) // { // NSLog(@"--------%@",group); // [groups addObject:group]; // } // failureBlock:nil]; //为了解决iOS8.0以上系统下无法通过代码创建被删除过的同名相册 [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^ { [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:@"琳小兮"]; } completionHandler:^(BOOL success, NSError *error) { if (!success) { NSLog(@"---error:%@",error.userInfo); } }]; haveHDRGroup = YES; } } }; [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:listGroupBlock failureBlock:nil]; [self saveToAlbumWithMetadata:nil imageData:UIImagePNGRepresentation(_imageV.image) customAlbumName:@"北企星" completionBlock:^ { //这里可以创建添加成功的方法 NSLog(@"-------新相册创建成功!!"); } failureBlock:^(NSError *error) { //处理添加失败的方法显示alert让它回到主线程执行,不然那个框框死活不肯弹出来 dispatch_async(dispatch_get_main_queue(), ^{ //添加失败一般是由用户不允许应用访问相册造成的,这边可以取出这种情况加以判断一下 if([error.localizedDescription rangeOfString:@"User denied access"].location != NSNotFound ||[error.localizedDescription rangeOfString:@"用户拒绝访问"].location!=NSNotFound){ UIAlertView *alert=[[UIAlertView alloc]initWithTitle:error.localizedDescription message:error.localizedFailureReason delegate:nil cancelButtonTitle:NSLocalizedString(@"ok", nil) otherButtonTitles: nil]; [alert show]; } }); }]; } - (void)saveToAlbumWithMetadata:(NSDictionary *)metadata imageData:(NSData *)imageData customAlbumName:(NSString *)customAlbumName completionBlock:(void (^)(void))completionBlock failureBlock:(void (^)(NSError *error))failureBlock { ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init]; void (^AddAsset)(ALAssetsLibrary *, NSURL *) = ^(ALAssetsLibrary *assetsLibrary, NSURL *assetURL) { [assetsLibrary assetForURL:assetURL resultBlock:^(ALAsset *asset) { [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) { if ([[group valueForProperty:ALAssetsGroupPropertyName] isEqualToString:customAlbumName]) { [group addAsset:asset]; if (completionBlock) { completionBlock(); } } } failureBlock:^(NSError *error) { if (failureBlock) { failureBlock(error); } }]; } failureBlock:^(NSError *error) { if (failureBlock) { failureBlock(error); } }]; }; [assetsLibrary writeImageDataToSavedPhotosAlbum:imageData metadata:metadata completionBlock:^(NSURL *assetURL, NSError *error) { if (customAlbumName) { [assetsLibrary addAssetsGroupAlbumWithName:customAlbumName resultBlock:^(ALAssetsGroup *group) { if (group) { [assetsLibrary assetForURL:assetURL resultBlock:^(ALAsset *asset) { [group addAsset:asset]; if (completionBlock) { completionBlock(); } } failureBlock:^(NSError *error) { if (failureBlock) { failureBlock(error); } }]; } else { AddAsset(assetsLibrary, assetURL); } } failureBlock:^(NSError *error) { AddAsset(assetsLibrary, assetURL); }]; } else { if (completionBlock) { completionBlock(); } } }]; }
时间: 2024-10-08 15:58:54