deleting a camera roll asset using Photos framewor

2019-04-09 07:43发布

I'm writing a test application to see if it is possible to delete a "photo library" asset in iOS 8, using the Photos framework. Although I suspect it is not possible, I find the documentation to be unclear, and there are posts on this site that seem to indicate it is possible. See here for example.

In my test app, I pick an asset from the library:

var picker = UIImagePickerController()
picker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
picker.mediaTypes = [kUTTypeMovie]

In the delegate callback, I get the NSURL of the asset, and put it into an array:

let thePicked = info[UIImagePickerControllerMediaURL] as NSURL!
var videosToDelete: [NSURL] = [theURL]

And here is the part I'm least confident about, where I get the PhotoLibrary, and do a change request to delete the asset:

PHPhotoLibrary.sharedPhotoLibrary().performChanges( {
    let assetToDelete = PHAsset.fetchAssetsWithALAssetURLs(videosToDelete, options: nil)
    PHAssetChangeRequest.deleteAssets(assetToDelete)
},
    completionHandler: { success, error in
        NSLog("Finished deleting asset. %@", (success ? "Success" : error))
})

The completion handler returns successfully, but the asset is never deleted. Am I doing something wrong? Or am I trying to do something inherently not allowed, and I'm not receiving the correct feedback for it?

1条回答
We Are One
2楼-- · 2019-04-09 08:02

Let me make sure I understand the question:

1) You use the photos framework to delete an asset with PHAssetChangeRequest.deleteAssets(assetToDelete)

2) you go to the Photos app and view the Recently Deleted photos, but you do not see the image/asset which you just deleted in the trash in the photos app.

If this is what you are experiencing, it is because you have deleted an asset that is not in the users 'camera roll' but is a PHAsst from the photostream from another device. When you delete these assets, they are not sent to the trash and are deleted differently than a normal photo. I would recommend going to the photos app and deleting the same asset--if it is a photostream asset, you will see that it never goes into the trash.

If this isn't the issue you are facing, please try to provide more detail.

查看更多
登录 后发表回答