6/17/2009

[iPhone] Saving an Image to the Library from UIImageView

Photo LibraryImage by ahhyeah via Flickr

In my latest app I have a UIImageView which shows an Image that has been taken from the web.
I then wanted to give the user the possibility to save that shown image to his library.
At this point I was getting ready for some longer coding for this but it turned out to be very easy.

To start from the same line let me point out where we are:
I have the following:
UIImageView *imageView;
UIImage *myImage;
-(IBAction)savePicture;


This is how its done:

-(IBAction)savePicture
{
myImage = imageView.image;
/* I assign the image that is being displayed in imageView to myImage */


UIImageWriteToSavedPhotosAlbum
(myImage, self, @selector(image:didFinishSavingWithError:contextInfo:), self);
/* This is the only code needed to save it to the library. See, its easier than you thought. */


[myImage release];
/* Release the image as we don't need to touch that anymore. Always a good thing to release images as early as possible */

}

//This is the error check to make sure we have a nice handling of our saving process.
-(void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
// Lets create an UIAlertView to display that everything is fine.
// That way the user will know it has been actually saved.
NSString *msg = @"Saved!!!";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Saved." message:msg delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert show];
[alert release];
[str release];
}


And that's it.
If you have any questions, I can try helping you out.
Reblog this post [with Zemanta]

8 comments:

  1. Hi, this is very helpful. I think that ultimately, I will need to do something like this. I have a related question for you. I am trying to build a simple app that is basically a list of items (fruit, cars, toys, etc.) and then, once you touch each item's cell, it brings you to a slideshow of different photos of these items. I am still learning the ins and outs of Obj-C before trying this, but am I right in thinking that I would need to create a UITableView (of NSArray items) that leads into a UIImageView? Does that sound right? Would you happen to know of any good tutorials out there for this sort of thing? Thank you for your time.

    ReplyDelete
  2. Sounds right to me I guess. You could look up the Transitions tutorial from apple. As for the tables, I'm not a big fan of them (as for how they are coded) so I can't help you with those.

    ReplyDelete
  3. Hi..my name is jeet. just read ur above post. coud u plz tell me where this savePicture is connected to????

    ReplyDelete
  4. n i wud lik to knw if u r saving the image from UIWebView by the 2-3 sec action popup which comes when user clicks n hold over an image in a UIWebView?

    ReplyDelete
  5. You mean the (IBAction)savePicture? IBAction is an Interface Builder Action, so its connected to some sort of button that you have created with the Interface Builder. It doesn't realy make a difference however. It could also just be a (void).

    ReplyDelete
  6. Hi mate i am having a requirement to PDF into iPad by email - ability to systematically submit documents with the iPad owners’ credentials extracted from email, and an app to view pdf's and edit a pdf.
    If you could help me by anything u know please reply to praveen_jm@hotmail.com

    ReplyDelete
  7. Im trying to build a TableView with cells: red, blue, green...
    when i press red it came a red pictur. i want a button so I save the Image to the library.

    Witch connections??

    ReplyDelete
  8. How to Save the picture by the uesr defined name in PhotoGallery.

    ReplyDelete