简体   繁体   English

如何删除添加在特定相册中但不在其他相册中的照片?

[英]How to delete photos added in specific albums but not in others?

I wonder in swift and xcode: how do I delete photos added in specific albums but not in others.我想知道 swift 和 xcode:如何删除添加在特定相册中而不是其他相册中的照片。 For example photos added in camera rolls but not in other albums?例如添加在相机胶卷中但不在其他相册中的照片? I know in principle, apple make it only a reference link of the same photo in camera rolls in other album, so if delete in one, you will delete in all others.原则上我知道,苹果只把它作为其他相册相册中同一张照片的参考链接,所以如果在其中一个删除,您将在所有其他相册中删除。 But would it be possible to make any way around it?但是有没有可能绕过它? For example copy it as a file copy to new album with different names?例如将其作为文件副本复制到具有不同名称的新专辑? Any one have any idea how to work around with it ?任何人都知道如何解决它?

Thank you,谢谢,

You can use Photos framework to remove the photo from the specific album.您可以使用Photos框架从特定相册中删除照片。 You need to PHAssetCollection object (say album ) of that album and PHAsset of the photo (say asset ) which belongs to that album.您需要该相册的PHAssetCollection对象(例如album )和属于该专辑的照片(例如asset )的PHAsset

In order to remove the asset from the particular album, you need to create PHAssetCollectionChangeRequest object (say request ) with that album and then delete the asset with the help of request using method removeAssets .为了从特定专辑中删除资产,您需要使用该album创建PHAssetCollectionChangeRequest对象(比如request ),然后在request的帮助下使用removeAssets方法删除asset

Sample样品

let album : PHAssetCollection = /*Your album object*/
let asset : PHAsset = /*Your Asset belongs to album*/
PHPhotoLibrary.shared().performChanges({
        guard let request = PHAssetCollectionChangeRequest(for: album) else {
            return
        }
        request.removeAssets([asset] as NSArray)
    }) { (result, error) in
        print("completionBlock",result, error)
    }

You can filter selected album and delete album like below您可以过滤所选专辑并删除专辑,如下所示

  if let assetCollection = album.filter({$0 == selectedAlbum}).first?.collection{
     PHPhotoLibrary.shared().performChanges({
       PHAssetCollectionChangeRequest.deleteAssetCollections([assetCollection] as NSArray)
        }, completionHandler: { (success, error) in
            if success {
               //enter code here
               Print("Successfully Deleted")
             }
            else if error = error {
              //enter error handler code here
              print(error)
             }
         }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM