Move PHAsset from one Album to Another (Swift)

431 Views Asked by At

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.

0

There are 0 best solutions below