objective c - Save a photo to custom album in iOS 8 -
i need little bit of in here, have method saves uiimage camera roll without problems in ios 8. method following
[[phphotolibrary sharedphotolibrary] performchanges:^{ [phassetchangerequest creationrequestforassetfromimage:image]; }completionhandler:^(bool success, nserror *error) { if(success){ nslog(@"worked"); }else{ nslog(@"error: %@", error); } }];
i need adapt code, image instead of saving uiimage camera roll, saves custom album named "myalbum"
i'm using photos.framework
you first need check album exists fetch request, , either add image album or create album , add image.
#import <photos/photos.h> - (void)addimagetocameraroll:(uiimage *)image { nsstring *albumname = @"myalbum"; void (^saveblock)(phassetcollection *assetcollection) = ^void(phassetcollection *assetcollection) { [[phphotolibrary sharedphotolibrary] performchanges:^{ phassetchangerequest *assetchangerequest = [phassetchangerequest creationrequestforassetfromimage:image]; phassetcollectionchangerequest *assetcollectionchangerequest = [phassetcollectionchangerequest changerequestforassetcollection:assetcollection]; [assetcollectionchangerequest addassets:@[[assetchangerequest placeholderforcreatedasset]]]; } completionhandler:^(bool success, nserror *error) { if (!success) { nslog(@"error creating asset: %@", error); } }]; }; phfetchoptions *fetchoptions = [[phfetchoptions alloc] init]; fetchoptions.predicate = [nspredicate predicatewithformat:@"localizedtitle = %@", albumname]; phfetchresult *fetchresult = [phassetcollection fetchassetcollectionswithtype:phassetcollectiontypealbum subtype:phassetcollectionsubtypeany options:fetchoptions]; if (fetchresult.count > 0) { saveblock(fetchresult.firstobject); } else { __block phobjectplaceholder *albumplaceholder; [[phphotolibrary sharedphotolibrary] performchanges:^{ phassetcollectionchangerequest *changerequest = [phassetcollectionchangerequest creationrequestforassetcollectionwithtitle:albumname]; albumplaceholder = changerequest.placeholderforcreatedassetcollection; } completionhandler:^(bool success, nserror *error) { if (success) { phfetchresult *fetchresult = [phassetcollection fetchassetcollectionswithlocalidentifiers:@[albumplaceholder.localidentifier] options:nil]; if (fetchresult.count > 0) { saveblock(fetchresult.firstobject); } } else { nslog(@"error creating album: %@", error); } }]; } }
Comments
Post a Comment