Thursday, July 14, 2011

Add Images To iPhone Simulator photo gallery

There are 2 ways to add image to iphone photo gallery:
  • Direct Method(drag and drop method).
  • Though coding.
Direct Method(drag and drop method):

Launch the simulator and go to this screen:

Drag the image which you want to add in gallery and drop on safari icon:


Click on image(anywhere inside the red rectangle in above image) and hold it:



You will get above 3 options: Save Image, Copy and Cancle.
Click on Save Image.
Now to see the saved image Go To below screen and click on photo:



 It will open the iPhone Album, select the 1st row of table, it will open all saved images.

Just double click on image to open.


Though coding : 
  1. Create a new project in xcode and name it as addImageInPhotoGallery.
  2. Add the image(Ref step 3 of .....) which you want to add in photo gallery in Resource folder of project.
  3. Open addImageInPhotoGallery.m, and write this code :

 - (void)viewDidLoad {
    
//suppose you have added photo.png in resource folder.
UIImage *sampleImage = [UIImage imageNamed:@"photo.png"];
    
UIImageWriteToSavedPhotosAlbum(sampleImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
   
 [super viewDidLoad];
}

- (void) image:(UIImage *) image didFinishSavingWithError:(NSError *) error contextInfo:(void *) contextInfo {
    UIAlertView *alert;
   
    // Unable to save the image 
    if (error)
        alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                           message:@"Unable to save image to Photo Album."
                                          delegate:self cancelButtonTitle:@"Ok"
                                 otherButtonTitles:nil];
    else // All is well
        alert = [[UIAlertView alloc] initWithTitle:@"Success"
                                           message:@"Image saved to Photo Album."
                                          delegate:self cancelButtonTitle:@"Ok"
                                 otherButtonTitles:nil];
   
   
    [alert show];
    [alert release];
   
}


4. Now just build and run this code.
 You will not see any thing on simulator screen, Go to photo gallery, open the Album and you will see  that photo is added to photo gallery.

4 comments:

  1. I am following your blog for quite a while. I followed your steps of code signing , but although everything worked fine. I guess p12 certificate thing is omitted. So please include how to handle p12 certification & exporting it.

    ReplyDelete
  2. @Rupaanjaan Thanks to notify me for this skipped part, I will try to include it ASAP.

    ReplyDelete
  3. hi archana,
    everything is fine but i selected the image from library. App is not working.
    And also i got this error "linker command failed with exit code 1 (use -v to see invocation)" when i tried to install application in device.and when i have testing in simulator its open a view when we scan a pic and automatic not dissmiss a view with qr code.Please send me the code to my mail (saritbahuguna74@gmail.com)

    ReplyDelete