【问题标题】:How to delete an Album on iOS?如何在 iOS 上删除相册?
【发布时间】:2017-12-14 02:24:10
【问题描述】:

我正在使用 PhotoKit 并且我已经创建了一个相册

PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle: RooPhotoAlbum.albumName)

但我发现第一次将文件保存到相册时有些问题 所以我需要删除它,这样我才能重现这个问题。 如何在 iOS 上以编程方式删除相册?

【问题讨论】:

    标签: ios swift photokit


    【解决方案1】:

    这是一个通过错误处理以编程方式删除自定义相册的函数:

    func deleteAlbum(albumName: String) {
        let options = PHFetchOptions()
        options.predicate = NSPredicate(format: "title = %@", albumName)
        let album = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: options)
    
        // check if album is available
        if album.firstObject != nil {
    
            // request to delete album
        PHPhotoLibrary.shared().performChanges({
            PHAssetCollectionChangeRequest.deleteAssetCollections(album)
        }, completionHandler: { (success, error) in
            if success {
                print(" \(albumName) removed succesfully")
            } else if error != nil {
                print("request failed. please try again")
            }
        })
        } else {
            print("requested album \(albumName) not found in photos")
        }
    }
    

    如何使用 -

    deleteAlbum(albumName: "YourAlbumName")
    

    【讨论】:

      【解决方案2】:
      let options = PHFetchOptions()
      options.predicate = NSPredicate(format: "title = %@", RootPhotoAlbum.albumName)
      let album = PHCollectionList.fetchCollectionLists(with: .folder, subtype: .any, options: options)
      
      PHPhotoLibrary.shared().performChanges({
          PHAssetCollectionChangeRequest.deleteAssetCollections([album] as NSArray)
      }, completionHandler: { (success, error) in
          if success {
              // Deleted successfully
          } else if let error = error {
              // Error deleting album
          }
      })
      

      【讨论】:

        【解决方案3】:

        Douglas 的答案获取 PHCollectionList 然后尝试对 PHAssetCollection 执行操作失败。你应该这样做:

        options.predicate = NSPredicate(format: "title = %@", "AlbumName")
        let album = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .albumRegular, options: options)
        
        PHPhotoLibrary.shared().performChanges({
            PHAssetCollectionChangeRequest.deleteAssetCollections(album)
        }, completionHandler: { (success, error) in
            if success {
                //success
            } else if let error = error {
                //failed
            }
        })
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-10-06
          • 2015-05-13
          • 2016-04-08
          • 2014-09-20
          • 2017-04-13
          相关资源
          最近更新 更多