iOS8开发之 PHAsset 保存图片到相册(相机胶卷) 详解三

http://blog.csdn.net/jerrychenly/article/details/44858355

解决iOS8下ALAssetsLibrary创建相册的bug

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

[objc]  view plain copy
  1. - (void)addAssetsGroupAlbumWithName:(NSString *)name resultBlock:(ALAssetsLibraryGroupResultBlock)resultBlock failureBlock:(ALAssetsLibraryAccessFailureBlock)failureBlock   

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



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

[objc]  view plain copy
  1. -(void)saveImageToXYAlbum:(UIImage*)image completion:(SaveImageCompletion)completion  
  2. {  
  3.     if(image){  
  4.         __weak ALAssetsLibrary *weakAssetsLibrary = assetsLibrary;  
  5.         __weak XYWallPaperHelper *weakSelf = self;  
  6.         dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{  
  7.               
  8.             if(TT_IS_IOS8_AND_UP){  
  9.                 PHPhotoLibrary* photoLibrary = [PHPhotoLibrary sharedPhotoLibrary];  
  10.                 [photoLibrary performChanges:^{  
  11.                     PHFetchResult* fetchCollectionResult;  
  12.                     PHAssetCollectionChangeRequest* collectionRequest;  
  13.                     NSString *albumIdentifier = [weakSelf xyWallPaperAlbumIdentifier];  
  14.                     if(albumIdentifier){  
  15.                         fetchCollectionResult = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[albumIdentifier] options:nil];  
  16.                         PHAssetCollection* exisitingCollection = fetchCollectionResult.firstObject;  
  17.                         collectionRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:exisitingCollection];  
  18.                     }else{  
  19.                         fetchCollectionResult = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[XYWallPaperAlbum] options:nil];  
  20.                         // Create a new album  
  21.                         if ( !fetchCollectionResult || fetchCollectionResult.count==0 ){  
  22.                             collectionRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:XYWallPaperAlbum];  
  23.                             [weakSelf saveXYWallPaperAblumIdentifier:collectionRequest.placeholderForCreatedAssetCollection.localIdentifier];  
  24.                         }  
  25.                     }  
  26.                     PHAssetChangeRequest* createAssetRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];  
  27.                     [collectionRequest addAssets:@[createAssetRequest.placeholderForCreatedAsset]];  
  28.                       
  29.                 } completionHandler:^(BOOL success, NSError *error){  
  30.                     completion(error);  
  31.                 }];  
  32.             }else{  
  33.                 [weakAssetsLibrary saveImage:image toAlbum:XYWallPaperAlbum withCompletionBlock:completion];  
  34.             }  
  35.         });  
  36.     }  
  37. }  

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

你可能感兴趣的:(iOS8开发之 PHAsset 保存图片到相册(相机胶卷) 详解三)