Speaking of Alassetslibrary, I believe that most of the iOS development know it, on iOS have done album related features should be more familiar with this, with it to operate access to the album is very convenient. However, since the release of IOS8, Alassetslibrary has been discussed on the stack overflow. Because in the iOS8 with this library to create a photo album there will be a bug: Create a photo album, manually delete it, and then use this library to create a newly deleted album, this time the method callback is not run fail module, it is the normal process, but the group is nil.
So all kinds of online search, also seeStack overflow A variety of solutions, I didn't find an effective solution anyway. This was inadvertently seen in the documentation:
So just follow the document to find out, and finally come to an effective solution, nonsense not much to say, directly affixed to the code (to the off-duty point).
-(void) Saveimagetoxyalbum: (uiimage*) Image completion: (saveimagecompletion) completion{if (image) {__weak alassets Library *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 fetchassetcollectionswithlocalident Ifiers:@[albumidentifier] Options:nil]; phassetcollection* exisitingcollection = Fetchcollectionresult.firstobject; CollEctionrequest = [Phassetcollectionchangerequest changerequestforassetcollection:exisitingcollection]; }else{Fetchcollectionresult = [Phassetcollection Fetchassetcollectionswithlocalidentifiers:@[xywal Lpaperalbum] Options:nil]; Create a new album if (!fetchcollectionresult | | fetchcollectionresult.count==0) { Collectionrequest = [Phassetcollectionchangerequest Creationrequestforassetcollectionwithtitle:xywallpapera Lbum]; [Weakself SaveXYWallPaperAblumIdentifier:collectionRequest.placeholderForCreatedAssetCollection.localIdentifier ]; }} phassetchangerequest* createassetrequest = [Phassetchangerequest creationrequest Forassetfromimage:image]; [Collectionrequest Addassets:@[createassetrequest.placeholderforcreatedasset]]; } Completionhandler:^ (BOOL success, Nserror *error) {completion (error); }]; }else{[weakassetslibrary saveimage:image toalbum:xywallpaperalbum withcompletionblock:completion]; } }); }}
Here's a place to be aware that you need to store localidentifier after creating a photo album, because it is needed the next time you get assetcollection. If you use the album name every time to try to get to the same name as previously created Assetcollection, will not get results. You need to pay attention here, the other is nothing, the code is relatively simple. The code section above is for iOS8 the following version of the code logic, here will not repeat, online a lot.
Fix bug that alassetslibrary create album under IOS8