Alice Chan
Alice Chan

Reputation: 2944

Swift: Delete photo in iOS Action Extension

So i'm developing an app to let user select a few photos in the system Photo Library then using the Action Extension via the share sheet to delete some of those photos.

I was using extensionContext.inputItems to get the URLs of the selected photos:

let extensionItems = (self.extensionContext!.inputItems as! [NSExtensionItem]).first!
let attachments = extensionItems.attachments! as! [NSItemProvider]

for provider in attachments {
    provider.loadItem(forTypeIdentifier: kUTTypeImage as String, options: nil) {
        (imageURL, error) in
        ...
        ...
        ...
    }
}

Then i was using PHAssetChangeRequest.deleteAssets to delete photos by it's URL

PHPhotoLibrary.shared().performChanges({
    let imageAssetToDelete = PHAsset.fetchAssets(withALAssetURLs: imageURLs, options: nil)
    PHAssetChangeRequest.deleteAssets(imageAssetToDelete)
}, completionHandler: {success, error in
    if success {
        print("success")
    } else {
        print(error!)
    }
})

The "success" was printed to the console but the photo deletion doesn't requested (that delete photo alert like this doesn't shown)

So i inspected the variable imageAssetToDelete but it prints:

<PHFetchResult: 0x28188b200> count=0

the PHFetchResult has no items.

Which part went wrong?

Upvotes: 2

Views: 378

Answers (1)

glassomoss
glassomoss

Reputation: 795

I've been struggling for days trying to find out what happened, but in my case the thing appeared to be extremely simple. In fact, changeBlock is called asynchronously (what is clearly stated in documentation) while in my code the array with assets is emptied right after (actually before) request. Code:

PHPhotoLibrary.shared().performChanges({
    // called when array is already empty
    PHAssetChangeRequest.deleteAssets(self.assetsToDelete as NSArray)
})
// called first
assetsToDelete.removeAll()

So the solution in my case was to simply move .removeAll() in completionHandler:

PHPhotoLibrary.shared().performChanges({
    PHAssetChangeRequest.deleteAssets(self.assetsToDelete as NSArray)
}, completionHandler: { c, _ in
    if c {
         self.assetsToDelete.removeAll()
    }
})

Upvotes: 0

Related Questions