Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save Photos to Custom Album in iPhones Photo Library

Tags:

I'm trying to create a custom album in the Photo Library of an iPhone and then save photos that I've taken with the camera, or chosen from the phones Camera Roll to that custom album. I can successfully create the album but the photos are not getting saved there, instead they are getting saved to the simulators Saved Photos album... I'm not sure how to tell UIImageWriteToSavedPhotosAlbum to save to the new album I've just created using addAssetsGroupAlbumWithName...

Here is the code I have so far - I've snipped out a few sections to keep my code example short...

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {        NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];   if([mediaType isEqualToString:(__bridge NSString *)kUTTypeImage])   {             // pull GPS information from photos metadata using ALAssetsLibrary     void (^ALAssetsLibraryAssetForURLResultBlock)(ALAsset *) = ^(ALAsset *asset)     {         // code snipped out      };     NSURL *assetURL = [info objectForKey:UIImagePickerControllerReferenceURL];     ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];     [library assetForURL:assetURL              resultBlock:ALAssetsLibraryAssetForURLResultBlock             failureBlock:^(NSError *error)              {                 // code snipped out             }];      // getimage from imagePicker and resize it to the max size of the iPhone screen      UIImage *originalImage = [info objectForKey:UIImagePickerControllerOriginalImage];      UIImage *resizedImage = [util_ createThumbnailForImage:originalImage thumbnailSize:[util_ determineIPhoneScreenSize]];     NSData *imageData = UIImagePNGRepresentation(resizedImage);                  // code snipped out                 // code snipped out                 // code snipped out                 // code snipped out                 // code snipped out                 // code snipped out        // create a new album called "My Apps Photos"     [library addAssetsGroupAlbumWithName:@"My Apps Photos"             resultBlock:^(ALAssetsGroup *group)              {                 NSLog(@"in addAssetsGroupAlbumWithName resultBlock");                  // save file to album                 UIImageWriteToSavedPhotosAlbum(resizedImage, self, nil, nil);              }              failureBlock:^(NSError *error)              {                 NSLog(@"in addAssetsGroupAlbumWithName failureBlock");              }      ];   } } 

So... Like I said, it creates the new album but does not save the photo there. How do I tell it to save into the new album? Perhaps I sound not use UIImageWriteToSavedPhotosAlbum??

Note: I'm using Xcode 4.3.2, IOS 5.1, and ARC

like image 720
ElasticThoughts Avatar asked Jun 08 '12 18:06

ElasticThoughts


People also ask

How do I save a photo to a specific album?

Step 1: Press and hold your finger over one photo to begin selecting photos for your album. Step 2: Once all are selected, tap on the more options menu icon (three dots in the top bar), then select Add to Album. Step 3: Tap on the album you would like to add the photos to.


1 Answers

If you are using iOS6, Fernando's answer will not work, because the saveImage selector is no longer available.

The process is pretty confusing, and I have not seen any clear answers posted, so here is the method I've used to solve this in iOS6.

You will need to use a combination of the following:

Create the Album:

[self.library addAssetsGroupAlbumWithName:albumName                                resultBlock:^(ALAssetsGroup *group) {          NSLog(@"added album:%@", albumName); }                              failureBlock:^(NSError *error) {          NSLog(@"error adding album"); }]; 

Find the Album:

__block ALAssetsGroup* groupToAddTo; [self.library enumerateGroupsWithTypes:ALAssetsGroupAlbum                              usingBlock:^(ALAssetsGroup *group, BOOL *stop) {       if ([[group valueForProperty:ALAssetsGroupPropertyName] isEqualToString:albumName]) {           NSLog(@"found album %@", albumName);           groupToAddTo = group;       } }                            failureBlock:^(NSError* error) {      NSLog(@"failed to enumerate albums:\nError: %@", [error localizedDescription]); }]; 

Save the Image to Asset Library, and put it into the album:

CGImageRef img = [image CGImage]; [self.library writeImageToSavedPhotosAlbum:img                                   metadata:[info objectForKey:UIImagePickerControllerMediaMetadata]                            completionBlock:^(NSURL* assetURL, NSError* error) {      if (error.code == 0) {          NSLog(@"saved image completed:\nurl: %@", assetURL);           // try to get the asset          [self.library assetForURL:assetURL                        resultBlock:^(ALAsset *asset) {               // assign the photo to the album               [groupToAddTo addAsset:asset];               NSLog(@"Added %@ to %@", [[asset defaultRepresentation] filename], albumName);           }                       failureBlock:^(NSError* error) {               NSLog(@"failed to retrieve image asset:\nError: %@ ", [error localizedDescription]);           }];      }      else {          NSLog(@"saved image failed.\nerror code %i\n%@", error.code, [error localizedDescription]);      }  }]; 
like image 136
Scott Allen Avatar answered Sep 28 '22 08:09

Scott Allen