解决iOS8下ALAssetsLibrary创建相册的bug

说到ALAssetsLibrary,相信搞iOS开发的大都知道它,在iOS上做过相册相关功能的应该对这个都比较熟悉了,用它来操作访问相册是很方便的。但是,,,,自从iOS8发布后,ALAssetsLibrary在Stack Overflow上被各种讨论了。因为在iOS8上用这个库创建相册会有个bug:创建相册,手动删除它,再用这个库去创建刚被删除的相册,这个时候方法回调是不会跑fail模块的,它走的是正常流程,但是带回来的group却是nil。

 

- (void)addAssetsGroupAlbumWithName:(NSString *)name resultBlock:(ALAssetsLibraryGroupResultBlock)resultBlock failureBlock:(ALAssetsLibraryAccessFailureBlock)failureBlock 
View Code

 


于是网上各种搜,也看到Stack Overflow各种各样的解决方法,反正我是没找到有效的解决办法。无意中在文档中看到了这个:

 

 

于是就顺着文档找下去,最后得出有效解决方法,废话不多说,直接贴代码了(到了下班点了)。

 

-(void)saveImageToXYAlbum:(UIImage*)image completion:(SaveImageCompletion)completion
{
    if(image){
        __weak ALAssetsLibrary *weakAssetsLibrary = assetsLibrary;
        __weak XYWallPaperHelper *weakSelf = self;
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            
            if(TT_IS_IOS8_AND_UP){
                PHPhotoLibrary* photoLibrary = [PHPhotoLibrary sharedPhotoLibrary];
                [photoLibrary performChanges:^{
                    PHFetchResult* fetchCollectionResult;
                    PHAssetCollectionChangeRequest* collectionRequest;
                    NSString *albumIdentifier = [weakSelf xyWallPaperAlbumIdentifier];
                    if(albumIdentifier){
                        fetchCollectionResult = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[albumIdentifier] options:nil];
                        PHAssetCollection* exisitingCollection = fetchCollectionResult.firstObject;
                        collectionRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:exisitingCollection];
                    }else{
                        fetchCollectionResult = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[XYWallPaperAlbum] options:nil];
                        // Create a new album
                        if ( !fetchCollectionResult || fetchCollectionResult.count==0 ){
                            collectionRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:XYWallPaperAlbum];
                            [weakSelf saveXYWallPaperAblumIdentifier:collectionRequest.placeholderForCreatedAssetCollection.localIdentifier];
                        }
                    }
                    PHAssetChangeRequest* createAssetRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
                    [collectionRequest addAssets:@[createAssetRequest.placeholderForCreatedAsset]];
                    
                } completionHandler:^(BOOL success, NSError *error){
                    completion(error);
                }];
            }else{
                [weakAssetsLibrary saveImage:image toAlbum:XYWallPaperAlbum withCompletionBlock:completion];
            }
        });
    }
}
View Code

 

 


这里有个地方需要注意,创建相册后要存储一下localIdentifier,因为下次再获取AssetCollection的时候需要用到它。如果每次都用相册名去试图获取到之前创建的同名AssetCollection,不会得到结果的。就这里需要注意一下,其他倒没什么,代码也比较简单。  上面代码部分针对iOS8以下版本的代码逻辑,这里就不赘述了,网上一大把。

 

转载自:http://blog.csdn.net/jerrychenly/article/details/44858355

posted @ 2015-04-07 15:07  luseike  阅读(351)  评论(0编辑  收藏  举报