iOS 无法创建名字跟已删除相册同名的相册问题

在IOS上,如果用ALAssertLibrary创建相册,如果该相册被删除过了,则再次创建,会创建成功,但是group为nil

[self addAssetsGroupAlbumWithName:albumName

                                                          resultBlock:^(ALAssetsGroup *group) {

                                                              

                                                              //get the photo's instance

                                                              [weakSelf assetForURL: assetURL

                                                                        resultBlock:^(ALAsset *asset) {

                                                                            

                                                                            //add photo to the newly created album

                                                                            [group addAsset: asset];

                                                                            

                                                                            //call the completion block

                                                                            completionBlock(nil);

                                                                            

                                                                        } failureBlock: completionBlock];

                                                              

                                                          } failureBlock: completionBlock];

在苹果的官方文档中,有一段说明,大概意思就是IOS8之后使用了新的库PHPhotoLibrary代替ALAssetsLibrary。

导入库Photo.framework,此时注意修改后面的选项为option,否则你的应用就必须支持IOS8以及以上了。

IOS8 创建相册

                                    [[PHPhotoLibrary sharedPhotoLibraryperformChanges:^{

                                        [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:albumName];

                                    } completionHandler:^(BOOL success, NSError *error) {

                                            } failureBlock:completionBlock];

                                        }

                                      

                                    }];


//下面是修改ALAssetsLibrary+CustomPhotoAlbum 的代码,兼容IOS8,在外调用依旧不变

-(void)addAssetURL:(NSURL*)assetURL toAlbum:(NSString*)albumName withCompletionBlock:(SaveImageCompletion)completionBlock

{

    __block BOOL albumWasFound = NO;

    

    //search all photo albums in the library

    [self enumerateGroupsWithTypes:ALAssetsGroupAlbum 

                        usingBlock:^(ALAssetsGroup *group, BOOL *stop) {


                            //compare the names of the albums

                            if ([albumName compare: [group valueForProperty:ALAssetsGroupPropertyName]]==NSOrderedSame) {

                                

                                //target album is found

                                albumWasFound = YES;

                                

                                //get a hold of the photo's asset instance

                                [self assetForURL: assetURL 

                                      resultBlock:^(ALAsset *asset) {

                                                  

                                          //add photo to the target album

                                          [group addAsset: asset];

                                          

                                          //run the completion block

                                          completionBlock(nil);

                                          

                                      } failureBlock: completionBlock];


                                //album was found, bail out of the method

                                return;

                            }

                            

                            if (group==nil && albumWasFound==NO) {

                                //photo albums are over, target album does not exist, thus create it

                                /*

                                 ( iOS 8.0 使用the Photos framework 代替 the Assets Library framework ,

                                 The Photos framework 提供更特色和更好的表现 在使用 photo library 工作的时候)

                                  iOS 8.0 之后相册新增了 Recently Deleted (最近删除这个功能导致我们在相册里边手动删除 "Rd"相册 是并没有完全删除导致创建相册虽然成功但却为 nil ,

                                 因为创建的相册还是处于 Recently Deleted 的状态;

                                 */

                                if (IS_IOS_8) {

                                    __weak ALAssetsLibrary* weakSelf = self;

                                    [[PHPhotoLibrary sharedPhotoLibraryperformChanges:^{

                                        [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:albumName];

                                    } completionHandler:^(BOOL success, NSError *error) {

                                        if (success) {

                                            [weakSelf enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

                                                if ([albumName compare: [group valueForProperty:ALAssetsGroupPropertyName]]==NSOrderedSame) {

                                                    [weakSelf assetForURL: assetURL

                                                          resultBlock:^(ALAsset *asset) {

                                                              

                                                              //add photo to the target album

                                                              [group addAsset: asset];

                                                              

                                                              //run the completion block

                                                              [self performSelectorOnMainThread:@selector(saveCompletionSuccess:) withObject:completionBlock waitUntilDone:YES];

                                                              

                                                          } failureBlock: completionBlock];

                                                }

                                            } failureBlock:completionBlock];

                                  

                                        }

                                      

                                    }];

                                }else{

                                    

                                    __weak ALAssetsLibrary* weakSelf = self;

                                    //create new assets album

                                    [self addAssetsGroupAlbumWithName:albumName

                                                          resultBlock:^(ALAssetsGroup *group) {

                                                              

                                                              //get the photo's instance

                                                              [weakSelf assetForURL: assetURL

                                                                        resultBlock:^(ALAsset *asset) {

                                                                            

                                                                            //add photo to the newly created album

                                                                            [group addAsset: asset];

                                                                            

                                                                            //call the completion block

                                                                            completionBlock(nil);

                                                                            

                                                                        } failureBlock: completionBlock];

                                                              

                                                          } failureBlock: completionBlock];

                                    

                                    //should be the last iteration anyway, but just in case

                                    return;

                                }

                                }

                    

                            

                        } failureBlock: completionBlock];

    

}



原文出处:http://blog.csdn.net/wangchenggggdn/article/details/46532931

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值