使用Photo框架

时间:2017-10-23 07:38:18

标签: ios swift photo phasset phphotolibrary

我正在编写一个相册应用,可以从用户的照片库中访问照片,添加过滤器和删除照片。我使用Photo框架删除了PHAssetChangeRequest.deleteAssets(assetToDelete)的资产。 此处的资产类别为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)
          })
        }
      }

但是错误发生在这里,“参数类型'PHAsset'不符合预期类型'NSFastEnumeration'”。

所以我按照Xcode推荐更改了assetToDelete的类型:

PHAssetChangeRequest.deleteAssets(assetToDelete as! NSFastEnumeration)

它仍然不起作用,错误显示:

  

无法将“PHAsset”类型的值转换为“NSFastEnumeration”

有谁知道如何处理这个问题?谢谢!

2 个答案:

答案 0 :(得分:4)

线索的名称为'assets'复数 - API需要一个数组或符合NSFastEnumeration的任何其他集合类型,例如Set

PHAssetChangeRequest.deleteAssets([assetToDelete])

https://developer.apple.com/documentation/photos/phassetchangerequest/1624062-deleteassets

答案 1 :(得分:2)

更正确的方法是先从库中获取:

let assetIdentifiers = assetsToDeleteFromDevice.map({ $0.localIdentifier })
let assets = PHAsset.fetchAssets(withLocalIdentifiers: assetIdentifiers, options: nil)
PHPhotoLibrary.shared().performChanges({
    PHAssetChangeRequest.deleteAssets(assets)
})