Delete Image from Photo Gallery

Delete image from Photo Gallery

fetchAssets(withALAssetURLs) - will be deprecated, what to use instead?
how to get image URL that is being saved to PhotoLibrary?

You don’t. That is why it is deprecated! You get the asset’s localIdentifier. This allows you to access the asset and delete it later.

Your completion handler is empty. That’s the problem. Write the completion handler and get the local identifier from the placeholder.

How to delete an image from photo library using UIImagePickerController

NSURL *url = [dict objectForKey:@"UIImagePickerControllerReferenceURL"] ;  

PHPhotoLibrary *library = [PHPhotoLibrary sharedPhotoLibrary];
[library performChanges:^{
// Here assetsURLs is array of url's you want to delete
PHFetchResult *assetsToBeDeleted = [PHAsset fetchAssetsWithALAssetURLs:[NSArray arrayWithObject:url] options:nil];
[PHAssetChangeRequest deleteAssets:assetsToBeDeleted];
} completionHandler:^(BOOL success, NSError *error)
{
// Check error and success here
}];

How to delete an image from PhotoLibrary after i pick it up using UIImagePickerController

Just to add to the above,
For swift 3.0 this worked for me.

PHPhotoLibrary.shared().performChanges({
let imageAssetToDelete = PHAsset.fetchAssets(withALAssetURLs: imageUrls as! [URL], options: nil)
PHAssetChangeRequest.deleteAssets(imageAssetToDelete)
}, completionHandler: {success, error in
print(success ? "Success" : error )
})

Delete a photo from the user's photo library?

Yes we can delete a photo. We can use PHAssetChangeRequest for this operation.

From Apple:

A request to create, delete, change metadata for, or edit the content of a Photos asset, for use in a photo library change block.

class func deleteAssets(_ assets: NSFastEnumeration)

where assets:
An array of PHAsset objects to be deleted.

PHAssetChangeRequest.deleteAssets([assetToDelete])

So, you could use the above code to delete assets.

below is swift 3 code,

PHPhotoLibrary.shared().performChanges({
let imageAssetToDelete = PHAsset.fetchAssets(withALAssetURLs: imageUrls as! [URL], options: nil)
PHAssetChangeRequest.deleteAssets(imageAssetToDelete)
}, completionHandler: {success, error in
print(success ? "Success" : error )
})

How to delete image from my Custom gallery and then Refresh the gallery

before calling notifyDataSetChanged(); You should remove Deleted image from thumbnails

you need a method in your Custom Adapter to do that

i don't know how to remove one item in Bitmap[] array. but if you can use List<Bitmap> instead of that, the method will be like this :

public void RemoveThumbnail(int position)
{
this.thumbnails.remove(position);
//notifyDataSetChanged() can be called in this method or after
//calling this method in MainActivity
notifyDataSetChanged();
}

otherwise this is something that removes thumbnail in specific position :

public void RemoveThumbnail(int position)
{
Bitmap[] temp = new Bitmap[thumbnails.length - 1];
int tempIndex = 0;
for (int i = 0 ; i < thumbnails.lenght ; i++)
{
if(i != position)
temp[tempIndex ++] = thumbnails[i];
}
thumbnails = temp;
//notifyDataSetChanged() can be called in this method or after
//calling this method in MainActivity
notifyDataSetChanged();
}


Related Topics



Leave a reply



Submit