Add assets to an iCloud shared photo album programmatically

丶灬走出姿态 提交于 2019-12-13 03:47:12

问题


Are there any limitations on using PhotoKit to add photos to Shared iCloud photo albums? While I can add new photos to AssetCollections that are regular albums, it does not work at all if the AssetCollection represents an iCloud sharing album. Do I also need to add a different type of PHAsset?

If I change the fetchAssetCollections call to use .albumRegular instead of .albumCloudShared, the code below works. Otherwise I get an error:

The operation couldn't be completed. (PHPhotosErrorDomain error -1.)

let sharedAlbums = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .albumCloudShared, options: nil)

let target = sharedAlbums.firstObject

PHPhotoLibrary.shared().performChanges({
    let assetChangeRequest = PHAssetChangeRequest.creationRequestForAsset(from: wantedimage)
    let assetPlaceHolder = assetChangeRequest.placeholderForCreatedAsset
    let albumChangeRequestShared = PHAssetCollectionChangeRequest(for: target!)
    let enumeration: NSArray = [assetPlaceHolder!]
    albumChangeRequestShared!.addAssets(enumeration)
}, completionHandler: { result, error in 
    print(result)
    print(error)
    print(error?.localizedDescription)
})


来源:https://stackoverflow.com/questions/59079357/add-assets-to-an-icloud-shared-photo-album-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!