I’m having hard time trying to figure out how to delete the custom albums made by my app along with its photos. NOTE: I can delete albums but not along with its photos.
THE CODE I’M BASING ON FROM STACKOVERFLOW: It works, but it deletes all the photos from the camera roll.
func deleteAllPhotos() {
let library = PHPhotoLibrary.shared()
library.performChanges({
let fetchOptions = PHFetchOptions()
let allPhotos = PHAsset.fetchAssets(with: .image, options: fetchOptions)
PHAssetChangeRequest.deleteAssets(allPhotos)
}) { (success, error) in
// Handle success & errors
}
}
MY MODIFIED CODE BASED ON THE ABOVE CODE:
let fetchOptions = PHFetchOptions()
fetchOptions.predicate = NSPredicate(format: “title = %@”, albumName)
let assetCollections = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions)
let album: PHAssetCollection = assetCollections.firstObject!
let allPhotos = PHAsset.fetchAssets(with: .image, options: fetchOptions)
PHPhotoLibrary.shared().performChanges({
guard let albumChangeRequest = PHAssetCollectionChangeRequest(for: (album)) else { return }
let fastEnumeration = NSArray(array: [allPhotos])
albumChangeRequest.removeAssets(fastEnumeration)
}, completionHandler: { success, error in
if success {
print("removed")
} else {
print("not removed")
}
})
ERROR: Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: 'Unsupported predicate in fetch options: title == "albumName"