Save a photo to a folder in photo library - ios

So I know that using the following saves an image to the general camera roll:
UIImage *image = imageView.image;
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
But what I want to do is save the photos to a specifically named folder in my photo library. For instance, any photo I take with Instagram is saved to an Instagram folder in my library. This also works for snapchat.

First you need to create a folder:
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library addAssetsGroupAlbumWithName:folderName
resultBlock:^(ALAssetsGroup *group)
{
NSLog(#"Added folder:%#", folderName);
}
failureBlock:^(NSError *error)
{
NSLog(#"Error adding folder");
}];
Then, find the folder:
__block ALAssetsGroup* folder;
[library enumerateGroupsWithTypes:ALAssetsGroupAlbum
usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
if ([[group valueForProperty:ALAssetsGroupPropertyName] isEqualToString:folderName])
{
folder = group;
}
}
failureBlock:^(NSError* error)
{
// Error handling.
}];
And add your photo to it.
Save the Image to Asset Library, and put it into the album:
[library writeImageToSavedPhotosAlbum:yourImage
metadata:[info objectForKey:UIImagePickerControllerMediaMetadata]
completionBlock:^(NSURL* assetURL, NSError* error)
{
if (error.code == 0)
{
// Get the asset
[library assetForURL:assetURL
resultBlock:^(ALAsset *asset)
{
// Assign the photo to the album
[folder addAsset:asset];
}
failureBlock:^(NSError* error)
{
// Error handling.
}];
}
else
{
// Error handling.
}
}];

Related

If I delete an image in my application it has to be deleted in the gallery too (using objective C)

I have developed a camera application with all basic functionalities. When you take a picture that image will be saved in the newly created folder but if I delete an image in my application its not getting deleted in that folder. I need to delete it permanently using objective C
You can't delete something from gallery, you can just use photos from gallery in your app and save to Gallery, but iOS doesn't give such permissions to other app as deleting something from Gallery.(if it is not jailbroken) There is no such functionality in WhatsApp too(or I can't see it).
FOR iOS 8.0+
You can only delete the ALAsset which is created by your app with document API [ALAsset setImageData:metadata:completionBlock:]
Saving Image photo.jpg to gallery by,
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);
}];
You can Find this in "Saved Photos" album.
To delete:
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) {
}];
ALAssetLibrary is Deprecated since iOS 9.0, you can use
PHPhotoLibrary Instead.
iOS 9.0+
// Delete asset from library
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
[PHAssetChangeRequest deleteAssets:assetArray];
} completionHandler:completionHandler];
In collection view didselect method i getting the url of selected image
UIImage *viewImage = _myma; // --- mine was made from drawing context
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
// Request to save the image to camera roll
[library writeImageToSavedPhotosAlbum:[viewImage CGImage] orientation:(ALAssetOrientation)[viewImage imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
if (error) {
NSLog(#"error");
} else {
NSLog(#"url %#", assetURL);
NSString *urlString = [assetURL absoluteString];
selectedurl = urlString;
//NSLog(#"myurl%#", selectedurl);
}
the in action button used to delete the selected images
NSURL *deleteurl = [NSURL URLWithString:selectedurl];
NSArray *arrDelete = [[NSArray alloc] initWithObjects:deleteurl , nil];
NSEnumerator *asset = [PHAsset fetchAssetsWithALAssetURLs:arrDelete options:nil];
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
[PHAssetChangeRequest deleteAssets:asset];// asset is an array
} completionHandler:^(BOOL success, NSError *error) {
NSLog(#"Finished Delete asset. %#", (success ? #"Success." : error));
if (success) {
NSLog(#"deleted");
}
}];

Save photos to album iOS, do not save all of them

I am trying to save photos from a app ,create the album and save the photos to it, but it don't works properly.
Sometime it save one or another photo, but never all of them.
Best regards to everyone and thanks!
for(PFObject *pf in imageFilesArray)
{
PFObject *imageObject = pf;
PFFile *imageFile = [imageObject objectForKey:#"image"];
[imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
UIImage *image = [UIImage imageWithData:data];
[self.library writeImageToSavedPhotosAlbum:[image CGImage] orientation:ALAssetOrientationUp completionBlock:^(NSURL *assetURL, NSError *error) {
if(assetURL) {
[self.library assetForURL:assetURL resultBlock:^(ALAsset *asset) {
[self addAsset:asset toGroup:#"App Album" inLib:self.library];
NSLog(#"Adicionado");
} failureBlock:^(NSError *error) {
NSLog(#"%#",error);
}];
}
}];
}
}];
}
[hud hide:NO];
The - (void) addAsset:(ALAsset*)a toGroup:(NSString*)name inLib:(ALAssetsLibrary*)lib method
[lib enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if([[group valueForProperty:ALAssetsGroupPropertyName] isEqualToString:name]) {
[group addAsset:a];
*stop = YES;
NSLog(#"added asset to EXISTING group");
}
if(!group) {
[lib addAssetsGroupAlbumWithName:name resultBlock:^(ALAssetsGroup *group) {
[group addAsset:a];
NSLog(#"added asset to NEW group");
} failureBlock:^(NSError *error) {
NSLog(#"e: %#", error);
}];
}
} failureBlock:^(NSError *error) {
NSLog(#"e: %#", error);
}];
I've discussed this issue with someone HERE.
... when there's no photo album available, only part images are saved in a for loop. I tested the code and found that -addAssetsGroupAlbumWithName:resultBlock:failureBlock: will process asynchrony, this method will be dispatched several times when the album is not created (this might take some time).
In this case, you can just create a photo album before image adding process.
[self.assetsLibrary addAssetsGroupAlbumWithName:<your_custom_album_name>
resultBlock:nil
failureBlock:nil];
And feel free to take a look at this lib: ALAssetsLibrary-CustomPhotoAlbum, it might do lots helpful things for u ;)

saving image to custom photo album in iOS7

I'm trying to save a photo taken with the camera to a custom photo album in iOS7.
my code looks like this:
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
__block ALAssetsGroup* folder;
[library enumerateGroupsWithTypes:ALAssetsGroupAlbum
usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
if ([[group valueForProperty:ALAssetsGroupPropertyName] isEqualToString:#"UrbanAlphabets"])
{
folder = group;
}
}
failureBlock:^(NSError* error)
{
// Error handling.
}];
[library writeImageToSavedPhotosAlbum:(__bridge CGImageRef)(image)
metadata:nil
completionBlock:^(NSURL* assetURL, NSError* error)
{
if (error.code == 0)
{
// Get the asset
[library assetForURL:assetURL
resultBlock:^(ALAsset *asset)
{
// Assign the photo to the album
[folder addAsset:asset];
NSLog(#"success");
}
failureBlock:^(NSError* error)
{
// Error handling.
NSLog(#"error1");
}];
}
else
{
// Error handling.
NSLog(#"error");
}
}];
and actually the console logs "success", so I guess everything should be fine, but it doesn't put the photo into the folder... I pretty much copypasted the code from here
http://www.ggkf.com/iphone/save-a-photo-to-a-folder-in-photo-library
any ideas?
this made it work
//write to photo library
NSString *albumName=#"Urban Alphabets";
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
__block ALAssetsGroup* groupToAddTo;
[library enumerateGroupsWithTypes:ALAssetsGroupAlbum
usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if ([[group valueForProperty:ALAssetsGroupPropertyName] isEqualToString:albumName]) {
groupToAddTo = group;
}
}
failureBlock:^(NSError* error) {
}];
CGImageRef img = [croppedImage CGImage];
[library writeImageToSavedPhotosAlbum:img
metadata:nil
completionBlock:^(NSURL* assetURL, NSError* error) {
if (error.code == 0) {
// try to get the asset
[library assetForURL:assetURL
resultBlock:^(ALAsset *asset) {
// assign the photo to the album
[groupToAddTo addAsset:asset];
}
failureBlock:^(NSError* error) {
}];
}
else {
}
}];

Creating image folders in IOS

All,
When the user of my app takes a photo, it will be saved but i want it to save in a new folder called 'Jasons Photos' and not the usual Camera Roll album.
I know of the completion block called save image:image toAlbum 'Jasons Album' with completion block, but I get an error complaining that 'No visible #interface for AlAssetslibrary declares the selector 'save image to album completion block' and its driving me crazy.
I have created an #property for the ALAssets library and i have included <AssetsLibrary> in my project.
thanks
You first need to write it to the savedPhotos, then get the ALAsset and add it to a new group
#import "QBViewController.h"
#import <AssetsLibrary/AssetsLibrary.h>
#implementation QBViewController
- (void)viewDidLoad
{
[super viewDidLoad];
ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init];
[lib writeImageToSavedPhotosAlbum:[[UIImage imageNamed:#"polter"] CGImage] orientation:ALAssetOrientationUp completionBlock:^(NSURL *assetURL, NSError *error) {
if(assetURL) {
[lib assetForURL:assetURL resultBlock:^(ALAsset *asset) {
[self addAsset:asset toGroup:#"forest" inLib:lib];
} failureBlock:^(NSError *error) {
NSLog(#"e: %#", error);
}];
}
else {
NSLog(#"e: %#", error);
}
}];
}
- (void) addAsset:(ALAsset*)a toGroup:(NSString*)name inLib:(ALAssetsLibrary*)lib {
[lib enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if([[group valueForProperty:ALAssetsGroupPropertyName] isEqualToString:name]) {
[group addAsset:a];
*stop = YES;
NSLog(#"added asset to EXISTING group");
}
if(!group) {
[lib addAssetsGroupAlbumWithName:name resultBlock:^(ALAssetsGroup *group) {
[group addAsset:a];
NSLog(#"added asset to NEW group");
} failureBlock:^(NSError *error) {
NSLog(#"e: %#", error);
}];
}
} failureBlock:^(NSError *error) {
NSLog(#"e: %#", error);
}];
}
#end

how to save UIImage in photos album using a custom name for the image

i am trying to save UIImage in the photos album of iPad with a custom name for each UIImage. I searched a lot and i realised that it is possible if i save them at the documents directory but not in the photos album. I was wondering if you could suggest a solution for that , please? Thank you in advance, best regards
To save it with a custom name I would use the following code:
// I do this in the didFinishPickingImage:(UIImage *)img method
// Build NSData in memory from the btnImage...
NSData* imageData = UIImageJPEGRepresentation(img, 1.0);
// Save to the default Apple (Camera Roll) folder.
[imageData writeToFile:#"/private/var/mobile/Media/DCIM/100APPLE/customImageFilename.jpg" atomically:NO];
In order to save an screenshot in the device in a custom album name use the following code:
- (void)createScreenshotAndSaveInACustomAlbum {
DebugLog(#"");
if ([[UIScreen mainScreen] respondsToSelector:#selector(scale)]) {
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
} else {
UIGraphicsBeginImageContext(self.view.bounds.size);
}
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library addAssetsGroupAlbumWithName:#"My Photo Album" resultBlock:^(ALAssetsGroup *group) {
// Checks if group previously created
if(group == nil){
// Enumerate albums
[library enumerateGroupsWithTypes:ALAssetsGroupAlbum
usingBlock:^(ALAssetsGroup *g, BOOL *stop)
{
// If the album is equal to our album
if ([[g valueForProperty:ALAssetsGroupPropertyName] isEqualToString:#"My Photo Album"]) {
// Save image
[library writeImageDataToSavedPhotosAlbum:UIImagePNGRepresentation(image) metadata:nil
completionBlock:^(NSURL *assetURL, NSError *error) {
// Then get the image asseturl
[library assetForURL:assetURL resultBlock:^(ALAsset *asset) {
// Put it into our album
[g addAsset:asset];
} failureBlock:^(NSError *error) {
}];
}];
}
} failureBlock:^(NSError *error){ }];
} else {
// Save image directly to library
[library writeImageDataToSavedPhotosAlbum:UIImagePNGRepresentation(image) metadata:nil
completionBlock:^(NSURL *assetURL, NSError *error) {
[library assetForURL:assetURL resultBlock:^(ALAsset *asset) {
[group addAsset:asset];
} failureBlock:^(NSError *error) { }];
}];
}
} failureBlock:^(NSError *error) { }];
}
Hope this helps!

Resources