Saving a View as a Photo in an iPhone App

For an iPhone app that I'm working on, I want to be able to save the screen image to the Photos album. My first attempt at this was complicated: I created a color space, a bitmap context, a CGImage, and finally a UIImage, copying and pasting most of the code from the Quartz 2D Programming Guide. Unfortunately, it didn't work; I kept getting BAD_ACCESS signals when I called UIImageWriteToSavedPhotosAlbum(), even though it looked to me like everything was correct.

After Googling for a bit for known issues with UIImageWriteToSavedPhotosAlbum, I ran across a far easier solution to the problem. Here are the methods I ended up with:

// Create an image for the view and save it to the Photos library
- (void)savePhotoOfView:(UIView *)view
{
    UIGraphicsBeginImageContext(view.bounds.size);
    [view drawRect:view.bounds];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    UIImageWriteToSavedPhotosAlbum(image,
                                   self,
                                   @selector(savedPhotoImage:didFinishSavingWithError:contextInfo:),
                                   NULL);
}

// Called by UIImageWriteToSavedPhotosAlbum() when it completes
- (void)   savedPhotoImage:(UIImage *)image
  didFinishSavingWithError:(NSError *)error
               contextInfo:(void *)contextInfo
{    
    NSString *message = @"This image has been saved to your Photos album";
    if (error) {
        message = [error localizedDescription];
    }
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
                                                    message:message
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
    [alert release];
}

These just call the view's drawRect method to create an image, save the image to the Photos library, and then pop up an alert box to let the user know what happened.

© 2003-2023 Kristopher Johnson