[英]How to delete photos added in specific albums but not in others?
我想知道 swift 和 xcode:如何刪除添加在特定相冊中而不是其他相冊中的照片。 例如添加在相機膠卷中但不在其他相冊中的照片? 原則上我知道,蘋果只把它作為其他相冊相冊中同一張照片的參考鏈接,所以如果在其中一個刪除,您將在所有其他相冊中刪除。 但是有沒有可能繞過它? 例如將其作為文件副本復制到具有不同名稱的新專輯? 任何人都知道如何解決它?
謝謝,
您可以使用Photos
框架從特定相冊中刪除照片。 您需要該相冊的PHAssetCollection
對象(例如album
)和屬於該專輯的照片(例如asset
)的PHAsset
。
為了從特定專輯中刪除資產,您需要使用該album
創建PHAssetCollectionChangeRequest
對象(比如request
),然后在request
的幫助下使用removeAssets
方法刪除asset
。
樣品
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)
}
您可以過濾所選專輯並刪除專輯,如下所示
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.