Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving image to custom album only

Tags:

xcode

ios

iphone

I've been following this tutorial http://www.touch-code-magazine.com/ios5-saving-photos-in-custom-photo-album-category-for-download/ to save an image to a custom album. This works fine but it's saving to both the camera roll and my custom album.

Looking at the code it seems like this is a necessary step, as after saving the image to the camera roll we have an ALAsset that can be used with the ALAssetsGroup addAsset: method.

Is there a way that I can add an image to a custom album without adding it to the camera roll?

Currently using this code:

-(void)saveImage:(UIImage*)image toAlbum:(NSString*)albumName withCompletionBlock:(SaveImageCompletion)completionBlock
{

 //write the image data to the assets library (camera roll)
    [self writeImageToSavedPhotosAlbum:image.CGImage orientation:(ALAssetOrientation)image.imageOrientation 
                        completionBlock:^(NSURL* assetURL, NSError* error) {

                      //error handling
                      if (error!=nil) {
                          completionBlock(error);
                          return;
                      }

                      //add the asset to the custom photo album
                      [self addAssetURL: assetURL 
                                toAlbum:albumName 
                    withCompletionBlock:completionBlock];

                  }];
}

-(void)addAssetURL:(NSURL*)assetURL toAlbum:(NSString*)albumName withCompletionBlock:(SaveImageCompletion)completionBlock
{
__block BOOL albumWasFound = NO;

//search all photo albums in the library
[self enumerateGroupsWithTypes:ALAssetsGroupAlbum 
                    usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

                        //compare the names of the albums
                        if ([albumName compare: [group valueForProperty:ALAssetsGroupPropertyName]]==NSOrderedSame) {

                            //target album is found
                            albumWasFound = YES;

                            //get a hold of the photo's asset instance
                            [self assetForURL: assetURL 
                                  resultBlock:^(ALAsset *asset) {

                                      //add photo to the target album
                                      [group addAsset: asset];

                                      //run the completion block
                                      completionBlock(nil);

                                  } failureBlock: completionBlock];

                            //album was found, bail out of the method
                            return;
                        }

                        if (group==nil && albumWasFound==NO) {
                            //photo albums are over, target album does not exist, thus create it

                            __weak ALAssetsLibrary* weakSelf = self;

                            //create new assets album
                            [self addAssetsGroupAlbumWithName:albumName 
                                                  resultBlock:^(ALAssetsGroup *group) {

                                                      //get the photo's instance
                                                      [weakSelf assetForURL: assetURL 
                                                                    resultBlock:^(ALAsset *asset) {

                                                                        //add photo to the newly created album
                                                                        [group addAsset: asset];

                                                                        //call the completion block
                                                                        completionBlock(nil);

                                                                    } failureBlock: completionBlock];

                                                  } failureBlock: completionBlock];

                            //should be the last iteration anyway, but just in case
                            return;
                        }

                    } failureBlock: completionBlock];

}
like image 223
HHHH Avatar asked Jan 01 '13 19:01

HHHH


1 Answers

Turns out there isn't - images will always be written to the camera roll by default.

like image 55
HHHH Avatar answered Sep 28 '22 06:09

HHHH