使用照片框架从照片库中删除照片
Deleting photo from Photo Library using Photo framework
我正在编写一个相册应用程序,它可以访问用户照片库中的照片、添加过滤器和删除照片。我使用照片框架删除了 PHAssetChangeRequest.deleteAssets(assetToDelete)
的资产。
这里资产的class是PHAsset
.
// Delete the photo from library
@IBAction func deleteBtnPressed(_ sender: Any) {
let assetToDelete = self.asset
if let assetToDelete = assetToDelete
{
PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.deleteAssets(assetToDelete)
})
}
}
但是这里发生了错误,"Argument type 'PHAsset' does not conform to expected type 'NSFastEnumeration'"。
所以我将 assetToDelete 的类型更改为 Xcode 建议:
PHAssetChangeRequest.deleteAssets(assetToDelete as! NSFastEnumeration)
还是不行,报错是:
Could not cast value of type 'PHAsset' to 'NSFastEnumeration'
有人知道怎么处理吗?谢谢!
线索在名称“资产”的复数中 - API 想要一个数组或任何其他符合 NSFastEnumeration 的集合类型,例如 Set
PHAssetChangeRequest.deleteAssets([assetToDelete] as NSArray)
https://developer.apple.com/documentation/photos/phassetchangerequest/1624062-deleteassets
更正确的做法应该是先从库中取:
let assetIdentifiers = assetsToDeleteFromDevice.map({ [=10=].localIdentifier })
let assets = PHAsset.fetchAssets(withLocalIdentifiers: assetIdentifiers, options: nil)
PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.deleteAssets(assets)
})
我正在编写一个相册应用程序,它可以访问用户照片库中的照片、添加过滤器和删除照片。我使用照片框架删除了 PHAssetChangeRequest.deleteAssets(assetToDelete)
的资产。
这里资产的class是PHAsset
.
// Delete the photo from library
@IBAction func deleteBtnPressed(_ sender: Any) {
let assetToDelete = self.asset
if let assetToDelete = assetToDelete
{
PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.deleteAssets(assetToDelete)
})
}
}
但是这里发生了错误,"Argument type 'PHAsset' does not conform to expected type 'NSFastEnumeration'"。
所以我将 assetToDelete 的类型更改为 Xcode 建议:
PHAssetChangeRequest.deleteAssets(assetToDelete as! NSFastEnumeration)
还是不行,报错是:
Could not cast value of type 'PHAsset' to 'NSFastEnumeration'
有人知道怎么处理吗?谢谢!
线索在名称“资产”的复数中 - API 想要一个数组或任何其他符合 NSFastEnumeration 的集合类型,例如 Set
PHAssetChangeRequest.deleteAssets([assetToDelete] as NSArray)
https://developer.apple.com/documentation/photos/phassetchangerequest/1624062-deleteassets
更正确的做法应该是先从库中取:
let assetIdentifiers = assetsToDeleteFromDevice.map({ [=10=].localIdentifier })
let assets = PHAsset.fetchAssets(withLocalIdentifiers: assetIdentifiers, options: nil)
PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.deleteAssets(assets)
})