Delete a photo from the user's photo library? - iphone

Is there a way I can delete an image that is loaded into my app from a UIImagePickerController?
I want to be able to delete the image from the user's photo library when the user performs a specific action.
I am prompting the user to choose a image from their library, then it gets loaded into my app at which point the app does some shnazzy animation, then actually deletes the image.
Please help!

Apple doesn't actually allow you to delete from the photo library through an API. The user has to actually go to the Photos app and delete it manually themselves. Apple does allow you write to the photo library:
To save a still image to the user’s
Saved Photos album, use the
UIImageWriteToSavedPhotosAlbum
function. To save a movie to the
user’s Saved Photos album, use the
UISaveVideoAtPathToSavedPhotosAlbum
function.
But for deleting and editing/overriding an existing photo, Apple doesn't have anything like that right now.

Actually, you can delete photos saved by your app (saved to photo library with UIImageWriteToSavedPhotosAlbum API call).
The documented API [ALAsset setImageData:metadata:completionBlock:] works.
1). Add an image "photo.jpg" to your project
2). Save an image to asset library:
ALAssetsLibrary *lib = [ALAssetsLibrary new];
UIImage *image = [UIImage imageNamed:#"photo.jpg"];
[lib writeImageToSavedPhotosAlbum:image.CGImage metadata:#{} completionBlock:^(NSURL *assetURL, NSError *error) {
NSLog(#"Write image %# to asset library. (Error %#)", assetURL, error);
}];
3). Go to default gallery, you will find photo.jpg in your "Saved Photos" album.
4). Delete this image from asset library:
ALAssetsLibrary *lib = [ALAssetsLibrary new];
[lib enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop) {
if(asset.isEditable) {
[asset setImageData:nil metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
NSLog(#"Asset url %# should be deleted. (Error %#)", assetURL, error);
}];
}
}];
} failureBlock:^(NSError *error) {
}];
5). Go to default gallery, you will find photo.jpg has already been deleted.

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 )
})

Related

save image in camera roll and get asset url

for an app I'm developing, I use UIImagePickerController to shoot a picture and store it in camera roll:
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info {
//... some stuff ...
UIImageWriteToSavedPhotosAlbum([info objectForKey:#"UIImagePickerControllerOriginalImage"], nil, nil, nil);
}
the image is saved, now I need to get its reference url so I try to enumerate camera roll and get the last image, but I always get the image before the one I just shot.
Anybody has an idea how to get the reference of the just saved picture?
Thanks,
Max
This solution should fix the problem:
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeImageToSavedPhotosAlbum:((UIImage *)[info objectForKey:UIImagePickerControllerOriginalImage]).CGImage
metadata:[info objectForKey:UIImagePickerControllerMediaMetadata]
completionBlock:^(NSURL *assetURL, NSError *error) {
NSLog(#"assetURL %#", assetURL);
}];
et voila:
assetURL assets-library://asset/asset.JPG?id=1611E84C-24E2-4177-B49A-1C57B4A9C665&ext=JPG

Request access to photo library during app launch

I'm working on an app that takes pictures automatically during certain events and saves them directly to the photo library. Unfortunately the user might not always be looking at their phone when this is running and the first time a picture is taken it asks for permission to access photos.
Is there a way I can force this request during launch and get it out of the way?
Thanks
Yes. You need to request access to the library.
Simply use this code in AppDelegate's didFinishLaunchingWithOptions
ALAssetsLibraryGroupsEnumerationResultsBlock assetGroupEnumerator =
^(ALAssetsGroup *assetGroup, BOOL *stop) {
if (assetGroup != nil) {
// do somthing
}
};
ALAssetsLibraryAccessFailureBlock assetFailureBlock = ^(NSError *error) {
LogError(#"Error enumerating photos: %#",[error description]);
};
NSUInteger groupTypes = ALAssetsGroupAll;
[library enumerateGroupsWithTypes:groupTypes usingBlock:assetGroupEnumerator failureBlock:assetFailureBlock];

ALAssetsLibrary delete ALAssetsGroup / ALAsset

I have created "photos album" from my App, using IOS AssetsLibrary.
Reading ALAssetsLibrary,ALAssetsGroup and ALAsset documentations, i have seen methods to "addAsset","addAssetsGroupAlbumWithName".
Is there a way to delete PROGRAMMATICALLY my ALAssetsGroup and ALAsset.
(the property 'editable' suppose to be TRUE because i create this data).
You can only delete the ALAsset which is created by your app with document API [ALAsset setImageData:metadata:completionBlock:] (But I have not found any API to delete a ALAssetGroup).
1). Add an image "photo.jpg" to your project
2). Save an image to asset library:
ALAssetsLibrary *lib = [ALAssetsLibrary new];
UIImage *image = [UIImage imageNamed:#"photo.jpg"];
[lib writeImageToSavedPhotosAlbum:image.CGImage metadata:#{} completionBlock:^(NSURL *assetURL, NSError *error) {
NSLog(#"Write image %# to asset library. (Error %#)", assetURL, error);
}];
3). Go to default gallery, you will find photo.jpg in your "Saved Photos" album.
4). Delete this image from asset library:
ALAssetsLibrary *lib = [ALAssetsLibrary new];
[lib enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop) {
if(asset.isEditable) {
[asset setImageData:nil metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
NSLog(#"Asset url %# should be deleted. (Error %#)", assetURL, error);
}];
}
}];
} failureBlock:^(NSError *error) {
}];
5). Go to default gallery, you will find photo.jpg has already been deleted.
This is not possible using any documented API. Only the photos app can delete Albums. If you want this feature to be added to iOS, I would fill a feature request at https://feedbackassistant.apple.com/.
in ios8 deleting photos might be possible using the Photos Framework
Please check the documentation of Photos Framework
For deleting assets refer to PHAssetChangeRequest
+ (void)deleteAssets:(id<NSFastEnumeration>)assets
where assets is an array of PHAsset objects to be deleted.
For deleting collections refer to PHAssetCollectionChangeRequest
+ (void)deleteAssetCollections:(id<NSFastEnumeration>)assetCollections
https://developer.apple.com/library/prerelease/ios/documentation/Photos/Reference/PHAssetChangeRequest_Class/index.html#//apple_ref/occ/clm/PHAssetChangeRequest/deleteAssets:
As Ted said, this is now possible in iOS 8 using the Photos service. It's pretty clean actually. You need to submit a change request to the photolibrary. Here's an example.
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
[PHAssetChangeRequest deleteAssets:arrayOfPHAssets];
} completionHandler:^(BOOL success, NSError *error) {
NSLog(#"Finished deleting asset. %#", (success ? #"Success." : error));
}];
Make sure you've imported Photos, and gotten authorization from the user. (Which you probably did to show the image already)
PHAssetchangeRequest - deleteAssets
https://developer.apple.com/library/prerelease/ios/documentation/Photos/Reference/PHAssetChangeRequest_Class/index.html#//apple_ref/occ/clm/PHAssetChangeRequest/deleteAssets:
PHPhotoLibrary Class - authorizationStatus
https://developer.apple.com/library/ios/documentation/Photos/Reference/PHPhotoLibrary_Class/#//apple_ref/occ/clm/PHPhotoLibrary/authorizationStatus
evanchin is correct. Further more, if you want to do this in Xamarin.iOS (aka monotouch):
var lib = new ALAssetsLibrary();
lib.Enumerate(ALAssetsGroupType.All, (ALAssetsGroup group, ref bool libStop) =>
{
if (group == null)
{
return;
}
group.Enumerate((ALAsset asset, int index, ref bool groupStop) =>
{
if (asset != null && asset.Editable)
{
asset.SetImageDataAsync(new NSData(IntPtr.Zero), new NSDictionary(IntPtr.Zero));
}
});
}, error => { });
This code will delete all images that your app added to the ALAssetsLibrary.
You may delete any asset in the library using documented API ONLY.
over writing the [ALAsset isEditable] function:
#implementation ALAsset(DELETE)
-(BOOL)isEditable{
return YES;
}
#end
like evanchin said, delete the asset:
ALAssetsLibrary *lib = [ALAssetsLibrary new];
[lib enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop) {
if(asset.isEditable) {
[asset setImageData:nil
metadata:nil
completionBlock:^(NSURL *assetURL, NSError *error) {
NSLog(#"Asset url %# should be deleted. (Error %#)", assetURL, error);
}];
}
}];
} failureBlock:^(NSError *error) {
}];

iPhone iOS how to extract photo metadata and geotagging info from a camera roll image? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How Do I Get The Correct Latitude and Longitude From An Uploaded iPhone Photo?
I'm making a photo app and would like to know what are my options for working with geotagged photos. I would like to display where a photo has been taken (similar to photos app). Is this possible?
Additionally, I need to know when the picture has been taken. I can capture this info for the pictures that I take myself, but what about the camera roll images?
Yes, it is possible.
You have to use ALAssetsLibrary to access your camera roll. Then you just enumerate through your photos and asks for location.
assetsLibrary = [[ALAssetsLibrary alloc] init];
groups = [NSMutableArray array];
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
if (group == nil)
{
return;
}
[groups addObject:group];
} failureBlock:^(NSError *error)
{
// Possibly, Location Services are disabled for your application or system-wide. You should notify user to turn Location Services on. With Location Services disabled you can't access media library for security reasons.
}];
This will enumerate your assets groups. Next, you pick up a group and enumerate its assets.
ALAssetGroup *group = [groups objectAtIndex:0];
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop)
{
if (result == nil)
{
return;
}
// Trying to retreive location data from image
CLLocation *loc = [result valueForProperty:ALAssetPropertyLocation];
}];
Now your loc variable contains location of the place where the photo was taken. You should check it against ALErrorInvalidProperty before use, since some photos might lack this data.
You can specify ALAssetPropertyDate to obtain the date and time of photo creation.

Properties of albums in ipad photo library

Is there any way to acccess the properties of the Albums synced in ipad photo library.I want to get the names with which the Album is saved in the photo library.Please anyone who have tries this or who have any knowledge in this case ,please help.
Thanks,
Christy
You'd want to use the ALAssetsLibrary. This ought to get you moving in the right direction:
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
if (group == nil)
return;
//this is the album name
NSLog(#"%#",[group valueForProperty:ALAssetsGroupPropertyName]);
}
failureBlock:^(NSError *error)
{
//failure code here
}];