2

I want to move a PHAsset from one Album to another album. Here is what I am doing:

func saveImage(image: UIImage, album: PhotoAlbum, completion: (PHFetchResult?)->()) {
        var placeholder: PHObjectPlaceholder?
        PHPhotoLibrary.sharedPhotoLibrary().performChanges({
            // Request creating an asset from the image
            let createAssetRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(image)
            // Request editing the album
            guard let albumChangeRequest = PHAssetCollectionChangeRequest(forAssetCollection: album) else {
                assert(false, "Album change request failed")
                return
            }
            // Get a placeholder for the new asset and add it to the album editing request
            guard let photoPlaceholder = createAssetRequest.placeholderForCreatedAsset else {
                assert(false, "Placeholder is nil")
                return
            }
            placeholder = photoPlaceholder
            albumChangeRequest.addAssets([photoPlaceholder])
            }, completionHandler: { success, error in
                guard let placeholder = placeholder else {
                    assert(false, "Placeholder is nil")
                    completion(nil)
                    return
                }

            if success {
                completion(PHAsset.fetchAssetsWithLocalIdentifiers([placeholder.localIdentifier], options: nil))
            }
            else {
                print(error)
                completion(nil)
            }
    })
}

The problem is that it creates a copy of it to destination rather than moving. At last, I ended up getting same images in different Albums.

Sohil R. Memon
  • 9,404
  • 1
  • 31
  • 57
  • Where does the UIImage parameter come from? Your function is only concerned with creating a new PHAsset from a UIImage. If there is another PHAsset that looks similar, you'd need a reference to that and then you can call create another change request and use deleteAsset to remove it, does that sound right or am I missing something? – Jessedc Sep 09 '16 at 03:29

0 Answers0