I need to delete the image from PhotoLibrary
. I am using UIImagePickerController
in 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()
}
4 个解决方案
#1
3
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 )
})
#2
6
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()
}
#3
4
Get image url....
得到图像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))
})
#4
1
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.asset
is a PHAsset
object (which can be acquired in several ways) referring to the photo you wish to delete. Don't forget to import the Photos framework!
自我的地方。asset是引用希望删除的照片的PHAsset对象(可以通过多种方式获取)。不要忘记导入照片框架!
Hope this helps!
希望这可以帮助!
#1
3
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 )
})
#2
6
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()
}
#3
4
Get image url....
得到图像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))
})
#4
1
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.asset
is a PHAsset
object (which can be acquired in several ways) referring to the photo you wish to delete. Don't forget to import the Photos framework!
自我的地方。asset是引用希望删除的照片的PHAsset对象(可以通过多种方式获取)。不要忘记导入照片框架!
Hope this helps!
希望这可以帮助!