How to Save Photos to Custom Album in iPhone/iPad Photo Library - Blogs
X
31Jul

How to Save Photos to Custom Album in iPhone/iPad Photo Library

Here is a way through which you can create your custom Album and can save captured images within that.

You can implement this feature using ALAssetsLibrary Class. 

First of all please add AssetsLibrary.framework into your project then create two class file which is below :-

 ALAssetsLibrary+CustomPhotoAlbum.h  and  ALAssetsLibrary+CustomPhotoAlbum.m

 ALAssetsLibrary+CustomPhotoAlbum.h

AlAssets

#import

#import

 

typedef void(^SaveImageCompletion)(NSError* error);

 

@interface ALAssetsLibrary(CustomPhotoAlbum)

 

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

-(void)addAssetURL:(NSURL*)assetURL toAlbum:(NSString*)albumName withCompletionBlock:(SaveImageCompletion)completionBlock;

 

@end

 

 

ALAssetsLibrary+CustomPhotoAlbum.m

 

 

#import "ALAssetsLibrary+CustomPhotoAlbum.h"

 

@implementation ALAssetsLibrary(CustomPhotoAlbum)

 

-(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

                                

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

    

}

 

@end

 

 

Now it's time to import these classes into you class file where you are going to use.

 

Import this class file into your class file by declaring this: 

 

#import

 

#import "ALAssetsLibrary+CustomPhotoAlbum.h"

 
Declare this  property in your .h class file 
@property (strong, atomic) ALAssetsLibrary* library;
 
Synthesize this in your .m class file 
 
@synthesize library;
 
Write your button action from where you want to launch camera and click pic.For instance i am declaring one method here.Suppose we have given this action on your  button so once button will get click it would open device camera
 
 
-(IBAction)capturePic:(id)sender

{

    

    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];

    imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;

imagePickerController.editing = YES;

    imagePickerController.delegate = (id)self;

    [self presentModalViewController:imagePickerController animated:YES];

 

}

 
Now it's time to set the custom name of your Album here  
 

#pragma -

#pragma mark Image picker delegate methdos

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo

{

 

   

    [self.library saveImage:image toAlbum:@"Chandra" withCompletionBlock:^(NSError *error) {

        if (error!=nil

        {

            NSLog(@"Big error: %@", [error description]);

        }

    }];

    

    [picker dismissModalViewControllerAnimated:NO];

    

}

 
Finally you are done with saving images into your Custom Album(Chandra).
 

Related

Connect apps and integrate data with workflows using Azure Logic Apps

Implementing complex business process has been made easy. Logic Apps in azure provide a way to imple...

Read More >

TFS to VSTS Migrations

The next evolution of TFSWhether you’re a startup or Enterprise or you’re a team of just...

Read More >

CfMD services -Dynamics NAV

CfMD services help ISVs minimize the time and effort needed to get their software solution through t...

Read More >

Upcoming Webinar - Offshore support to North American Dynamics NAV partners

Offshore support to North American Dynamics NAV partners whose customers are moving to NAV '16Wh...

Read More >

Microsoft Azure Consultant

·         Executing technical feasibility as...

Read More >

Customer Feedback

Your feedback is important to us. Please share your experience working with Canarys & the journe...

Read More >

Threading

Multi-taskingIt is a concept of performing multiple tasks over a certain period of time by executing...

Read More >

Partnership Program

We understand that the world of technology is changing rapidly from day to day and businesses have m...

Read More >

Partnership Program

The Canarys Partner Program:Canarys is all set to expand its business reach across the Globe and is ...

Read More >

Upcoming Webinar US - Plugging for Cloud, Right or Wrong

Plugging for Cloud, Right or Wrong?Cloud computing continues to be one of the most advertising topic...

Read More >

Share

Try DevOpSmartBoard Ultimate complete Azure DevOps End-to end reporting tool

Sign Up

  • Recent
  • Popular
  • Tag
Tags
Monthly Archive
Subscribe
Name

Text/HTML
Contact Us
  • *
  • *