Showing posts with label SDK. Show all posts
Showing posts with label SDK. Show all posts

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]