通过编程将资产添加到iCloud共享相册 [英] Add assets to an iCloud shared photo album programmatically
问题描述
使用PhotoKit将照片添加到Shared iCloud相册是否有任何限制?虽然我可以将新照片添加到作为常规相册的AssetCollections中,但是如果AssetCollection代表iCloud共享相册,则根本不起作用.我还需要添加其他类型的PHAsset吗?
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?
如果将fetchAssetCollections
调用更改为使用.albumRegular
而不是.albumCloudShared
,则下面的代码有效.否则我会报错:
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)
})
推荐答案
是的,您不能将图像添加到iCloud共享相册.
Yes, you can't add images to an iCloud shared album.
在执行更改请求之前,您应该在PHAssetCollection上调用canPerform(_:)
.
Prior to performing a change request you should call canPerform(_:)
on the PHAssetCollection.
如果在albumCloudShared
的PHAssetCollectionSubtype
上调用canPerform(_:)
,它将返回false.
If you call canPerform(_:)
on a PHAssetCollectionSubtype
of albumCloudShared
it will return false.
例如:
print(collection.canPerform(.createContent)) //false
print(collection.canPerform(.addContent)) //false
我遇到了同样的问题,并通过反馈"应用提交了功能请求.
I had this same problem and submitted a feature request via the Feedback app.
这篇关于通过编程将资产添加到iCloud共享相册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!