ios - Move/copy PHAsset from one album to another -


i want move asset 1 album another. find information on creating , deleting assets. reason want move instead of creating new 1 , deleting old copy is, want preserve bits of data localidentifier of asset. if recreate asset in album, assigned new localidentifier, right?

but seems it's not possible. resorted copying asset @ least. it's giving me trouble well.

here's code.

func copyasset() {     if let assets = getassetsfromcollection(collectiontitle: "old") {         if getassetcolection("new") == nil {             createcollection("new") { collection in                 if let collection = collection {                     print("collection created")                     self.copyassetstocollection(assets, tocollection: collection)                 }             }         }     } }  /**  copy assets album.   - parameter assets:     array of assets copy.  - parameter collection: collection assets need copied.  */ func copyassetstocollection(assets: [phasset], tocollection collection: phassetcollection) {     phphotolibrary.sharedphotolibrary().performchanges({         let asset = assets.first!         print(asset.localidentifier)         let assetchangerequest = phassetchangerequest(forasset: asset)         let assetplaceholder = assetchangerequest.placeholderforcreatedasset!          let collectionchangerequest = phassetcollectionchangerequest(forassetcollection: collection)!         collectionchangerequest.addassets([assetplaceholder])     }, completionhandler: { success, error in         if success {             print("photo copied collection")         } else {             if let error = error {                 print("error adding photos collection: \(error.code) - \(error.localizeddescription)")             }         }     }) }  /**  retrieve album given title.   - parameter title: album title.   - returns: album if exists. nil if doesn't.  */ func getassetcolection(title: string) -> phassetcollection? {     let fetchoptions = phfetchoptions()     fetchoptions.predicate = nspredicate(format: "title ==[cd] %@", title)     if let collection = phassetcollection.fetchassetcollectionswithtype(.album, subtype: .albumregular, options: fetchoptions).firstobject {         return collection as? phassetcollection     }     return nil }  /**  assets given album.   - parameter title: album title.   - returns: array of assets if exist. nil if don't.  */ func getassetsfromcollection(collectiontitle title: string) -> [phasset]? {     var assets = [phasset]()     if let collection = getassetcolection(title) {         let fetchoptions = phfetchoptions()         fetchoptions.predicate = nspredicate(format: "mediatype == %d", phassetmediatype.image.rawvalue)         let fetchresult = phasset.fetchassetsinassetcollection(collection, options: fetchoptions)         fetchresult.enumerateobjectsusingblock { object, index, stop in             if let asset = object as? phasset {                 assets.append(asset)             }         }         return assets     }     return nil }  /**  create album in photos app.   - parameter title:      album title.  - parameter completion: album if created. nil if fails.  */ func createcollection(title: string, completion: (collection: phassetcollection?) -> ()) {     var albumplaceholder: phobjectplaceholder!     phphotolibrary.sharedphotolibrary().performchanges({         let request = phassetcollectionchangerequest.creationrequestforassetcollectionwithtitle(title)         albumplaceholder = request.placeholderforcreatedassetcollection     }, completionhandler: { success, error in         if success {             let fetchresult = phassetcollection.fetchassetcollectionswithlocalidentifiers([albumplaceholder.localidentifier], options: nil)             completion(collection: fetchresult.firstobject as? phassetcollection)         } else {             if let error = error {                 print("failed create album: \(title) in photos app: \(error.code) - \(error.localizeddescription)")                 completion(collection: nil)             }         }     }) } 

the app crashes @ line let assetplaceholder = assetchangerequest.placeholderforcreatedasset! finding nil value placeholderforcreatedasset. means phassetchangerequest(forasset: asset) can used changing asset's metadata , not duplicating it.

is there other way this? workaround or hack? i'll appreciate help.

there nothing copying asset 1 album another: in ios photo library original photos/videos located in "all photos"/"camera roll". albums contain references original photo/video located in "all photos/camera roll". means can assign 1 photo n-albums. manage assignments please have @ phassetcollectionchangerequest class. add 1 or several photos/video @ album use addassets:, remove assets album use removeassets:method.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -