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"
    else // All is well
        alert = [[UIAlertView alloc] initWithTitle:@"Success"
                                           message:@"Image saved to Photo Album."
                                          delegate:self cancelButtonTitle:@"Ok"
    [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.