0
votes

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.

1

1 Answers

2
votes

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.
     }
 }];