ios - Creating a custom album on swift (a request is interfering on the process) -
i have following code got forum, , works perfectly. first time when creates album, requests user permission access photos. causes album doesn't create until second time application run.
how can control that?, in order album can created first time requested. sorry if english not good, know little bit. hope can me!
finally here code:
import photos var assetcollection: phassetcollection! var albumfound : bool = false var photosasset: phfetchresult! var assetthumbnailsize:cgsize! var collection: phassetcollection! var assetcollectionplaceholder: phobjectplaceholder! func createalbum() { let fetchoptions = phfetchoptions() fetchoptions.predicate = nspredicate(format: "title = %@", "myalbum") let collection : phfetchresult = phassetcollection.fetchassetcollectionswithtype(.album, subtype: .any, options: fetchoptions) if let first_obj: anyobject = collection.firstobject { self.albumfound = true assetcollection = collection.firstobject phassetcollection } else { phphotolibrary.sharedphotolibrary().performchanges({ var createalbumrequest : phassetcollectionchangerequest = phassetcollectionchangerequest.creationrequestforassetcollectionwithtitle("myalbum") self.assetcollectionplaceholder = createalbumrequest.placeholderforcreatedassetcollection }, completionhandler: { success, error in self.albumfound = (success ? true: false) if (success) { var collectionfetchresult = phassetcollection.fetchassetcollectionswithlocalidentifiers([self.assetcollectionplaceholder.localidentifier], options: nil) print(collectionfetchresult) assetcollection = collectionfetchresult?.firstobject phassetcollection } }) }}
instead of creating variable first_obj, use
if (collection.firstobject != nil) { } else { }
Comments
Post a Comment