将PHAsset从一个专辑移动到另一个专辑(Swift)

时间:2016-09-09 02:14:31

标签: swift swift2 phasset phassetcollection phassetslibrary

我想将PHAsset从一张专辑移到另一张专辑。这就是我在做的事情:

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)
            }
    })
}

问题是它会创建一份到目的地而不是移动的副本。最后,我最终在不同的专辑中获得相同的图像。

0 个答案:

没有答案