【问题标题】:I can't delete a custom album with its photo using PHPhotoLibrary我无法使用 PHPhotoLibrary 删除带有照片的自定义相册
【发布时间】:2019-12-19 22:40:23
【问题描述】:

我很难弄清楚如何删除我的应用制作的自定义相册及其照片。注意:我可以删除相册,但不能连同照片一起删除。

我基于 STACKOVERFLOW 的代码:它有效,但它会删除相机胶卷中的所有照片。

func deleteAllPhotos() {
    let library = PHPhotoLibrary.shared()
    library.performChanges({
        let fetchOptions = PHFetchOptions()
        let allPhotos = PHAsset.fetchAssets(with: .image, options: fetchOptions)
        PHAssetChangeRequest.deleteAssets(allPhotos)
    }) { (success, error) in
        // Handle success & errors
    }
}

我根据上述代码修改的代码:

let fetchOptions = PHFetchOptions()
fetchOptions.predicate = NSPredicate(format: "title = %@", albumName)

let assetCollections = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions)
let album: PHAssetCollection = assetCollections.firstObject!
let allPhotos = PHAsset.fetchAssets(with: .image, options: fetchOptions)

PHPhotoLibrary.shared().performChanges({
    guard let albumChangeRequest = PHAssetCollectionChangeRequest(for: (album)) else { return }
    let fastEnumeration = NSArray(array: [allPhotos])
            albumChangeRequest.removeAssets(fastEnumeration)
        }, completionHandler: { success, error in
        if success {
            print("removed")
        } else {
            print("not removed")
        }
    })

错误:由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“获取选项中不支持的谓词:title == “albumName”

【问题讨论】:

  • 显然没有人知道这个问题的答案,我想我会自己弄清楚。

标签: swift


【解决方案1】:

诀窍是将inalbum 一起用于fetchAssets

let allPhotos = PHAsset.fetchAssets(in: album!, options: nil)
PHAssetChangeRequest.deleteAssets(allPhotos)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-12
    • 1970-01-01
    • 2017-06-09
    相关资源
    最近更新 更多