【问题标题】:Deleting PHAssetCollection (Swift)删除 PHAssetCollection (Swift)
【发布时间】:2015-07-12 19:14:32
【问题描述】:

我创建了一个 PHAssetCollection 来存放我的应用程序的照片,它工作正常。但是,我试图拥有它,以便用户在按下按钮时可以删除 PHAssetCollection。如何删除我在以下代码中创建的整个 AssetCollection(“应用程序文件夹”)?



创建 PHAssetCollection 的代码:

    let albumName = "App Folder" 

    //Check if the folder exists, if not, create it
    let fetchOptions = PHFetchOptions()
    fetchOptions.predicate = NSPredicate(format: "title = %@", albumName)
    let collection:PHFetchResult = PHAssetCollection.fetchAssetCollectionsWithType(.Album, subtype: .Any, options: fetchOptions)

    if let first_Obj:AnyObject = collection.firstObject{
        //found the album
        self.albumFound = true
        self.assetCollection = first_Obj as! PHAssetCollection
    }else{
        //Album placeholder for the asset collection, used to reference collection in completion handler
        var albumPlaceholder:PHObjectPlaceholder!
        //create the folder
        NSLog("\nFolder \"%@\" does not exist\nCreating now...", albumName)
        PHPhotoLibrary.sharedPhotoLibrary().performChanges({
            let request = PHAssetCollectionChangeRequest.creationRequestForAssetCollectionWithTitle(albumName)
            albumPlaceholder = request.placeholderForCreatedAssetCollection
            },
            completionHandler: {(success:Bool, error:NSError!)in
                if(success){
                    println("Successfully created folder")
                    self.albumFound = true
                    if let collection = PHAssetCollection.fetchAssetCollectionsWithLocalIdentifiers([albumPlaceholder.localIdentifier], options: nil){
                        self.assetCollection = collection.firstObject as! PHAssetCollection
                    }
                }else{
                    println("Error creating folder")
                    self.albumFound = false
                }
        })

    }

【问题讨论】:

    标签: ios iphone swift phasset


    【解决方案1】:

    在 PHAssetCollectionChangeRequest 中有一个名为 deleteAssetCollections: 的类方法,它就是这样做的:请求删除特定的资产集合。查看文档,您似乎可以使用一组 PHAssetCollections 来调用它,如下所示:

    PHAssetCollectionChangeRequest.deleteAssetCollections(self.assetCollection)
    

    【讨论】:

    • 这不起作用,但很接近,它需要一个完成处理程序......我在更新的答案中找到了解决方案。
    【解决方案2】:
        PHPhotoLibrary.sharedPhotoLibrary().performChanges({ () -> Void in
    
        PHAssetCollectionChangeRequest.deleteAssetCollections([self.deleteTarget])
    
        }, completionHandler: nil)
    

    【讨论】:

      【解决方案3】:

      只是让它易于使用。希望能帮助到你 : 这是使用错误处理以编程方式删除自定义相册的功能

          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")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-10-19
        • 1970-01-01
        • 2015-08-14
        • 2015-03-27
        • 2018-05-05
        • 2023-03-21
        • 1970-01-01
        相关资源
        最近更新 更多