删除 PHAssetCollection (Swift)

Posted

技术标签:

【中文标题】删除 PHAssetCollection (Swift)【英文标题】:Deleting 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
                
        )

    

【问题讨论】:

【参考方案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")

【讨论】:

以上是关于删除 PHAssetCollection (Swift)的主要内容,如果未能解决你的问题,请参考以下文章

如何知道 PHAssetCollection 是不是是最近删除的集合?

创建相册的过滤 PHAssetCollection 失败

何时使用 PHAsset、PHCollection、PHAssetCollection 和 PHCollectionList

将 UIImage 保存到 PHAssetCollection

为啥 PHAssetCollection 计数为 0?

使用 PHFetchOptions.predicate 从 PHAssetCollection 获取 PHAsset 的问题