Fix bug that alassetslibrary create album under IOS8

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.