3

我创建了一个 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
                }
        })

    }
4

3 回答 3

1

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

PHAssetCollectionChangeRequest.deleteAssetCollections(self.assetCollection)
于 2015-07-12T22:04:22.567 回答
1
    PHPhotoLibrary.sharedPhotoLibrary().performChanges({ () -> Void in

    PHAssetCollectionChangeRequest.deleteAssetCollections([self.deleteTarget])

    }, completionHandler: nil)
于 2015-07-12T23:05:42.530 回答
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")
于 2019-10-23T05:52:27.690 回答