xcode 使用 UIImagePickerController 拾取图像后如何从 PhotoLibrary 中删除图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29982064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delete an image from PhotoLibrary after i pick it up using UIImagePickerController
提问by Dinesh Jeyasankar
I need to delete the image from PhotoLibrary. I am using UIImagePickerControllerin my application to pick up the image. I need to delete this image from iOS PhotoLibrary after i use it in my application.
我需要从PhotoLibrary. 我UIImagePickerController在我的应用程序中使用来拾取图像。在我的应用程序中使用它后,我需要从 iOS PhotoLibrary 中删除它。
My Code snippet
我的代码片段
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary)
{
var imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary;
imagePicker.allowsEditing = false
self.presentViewController(imagePicker, animated: true, completion: nil)
}
// MARK:- UIImagePickerControllerDelegate
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
pickedImage = image
saveImageToDisk(pickedImage)
/*
I need the logic to delete this image from PhotoLibrary here.
*/
self.dismissViewControllerAnimated(true, completion: nil)
refreshCollectionView()
}
采纳答案by Sri Hari YS
Just to add to the above, For swift 3.0 this worked for me.
只是为了补充上述内容,对于 swift 3.0 这对我有用。
PHPhotoLibrary.shared().performChanges({
let imageAssetToDelete = PHAsset.fetchAssets(withALAssetURLs: imageUrls as! [URL], options: nil)
PHAssetChangeRequest.deleteAssets(imageAssetToDelete)
}, completionHandler: {success, error in
print(success ? "Success" : error )
})
回答by Dinesh Jeyasankar
Thanks for the help.
谢谢您的帮助。
Fixed it with the below code.
用下面的代码修复它。
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
pickedImage = image
saveImageToDisk(pickedImage)
refreshCollectionView()
let imageUrl = info[UIImagePickerControllerReferenceURL] as! NSURL
var imageUrls = [imageUrl]
//Delete asset
PHPhotoLibrary.sharedPhotoLibrary().performChanges( {
let imageAssetToDelete = PHAsset.fetchAssetsWithALAssetURLs(imageUrls, options: nil)
PHAssetChangeRequest.deleteAssets(imageAssetToDelete)
},
completionHandler: { success, error in
NSLog("Finished deleting asset. %@", (success ? "Success" : error))
})
self.dismissViewControllerAnimated(true, completion: nil)
refreshCollectionView()
}
回答by Rahul Mayani
Get image url....
获取图片网址....
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
let imageURL = info[UIImagePickerControllerReferenceURL] as NSURL
let imageName = imageURL.path!.lastPathComponent
picker.dismissViewControllerAnimated(true, completion: nil)
}
delete the asset:
删除资产:
PHPhotoLibrary.sharedPhotoLibrary().performChanges( {
let imageAssetToDelete = PHAsset.fetchAssetsWithALAssetURLs(**imageUrl**, options: nil)
PHAssetChangeRequest.deleteAssets(imageAssetToDelete)
},
completionHandler: { success, error in
NSLog("Finished deleting asset. %@", (success ? "Success" : error))
})
回答by pankaj asudani
You can do it in following way:
您可以通过以下方式进行:
//under SamplePhotosApp/AAPLAssetViewController.m
// Delete asset from library
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
[PHAssetChangeRequest deleteAssets:@[self.asset]];
} completionHandler:completionHandler];
where self.assetis a PHAssetobject (which can be acquired in several ways) referring to the photo you wish to delete. Don't forget to import the Photos framework!
哪里self.asset是PHAsset指您要删除的照片的对象(可以通过多种方式获取)。不要忘记导入照片框架!
Hope this helps!
希望这可以帮助!

