tags:

views:

1244

answers:

3

Is there an easy way to save a view to the Photos library in an iPhone app?

+11  A: 

I add these methods to my view's controller class. They save the photo and then pop up an alert box to tell the user whether it succeeded.

- (void)savePhotoOfView:(UIView *)view
{
    UIGraphicsBeginImageContext(view.bounds.size);
    [view drawRect:view.bounds];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

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

- (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];
}
Kristopher Johnson
A: 

Forgive me for my stupidity, but how to I refer to the current view? In my case it's a custom detail view showing an item from a tableview.

I've tried self.view but all that gets saved is a blank image.

Any help appreciated.

matt
Without seeing your code, nobody can answer that. Kristophers code will render an image of any view you pass to it.
Roger Nolan
If self.view isn't right, maybe it's a subview that you want to render?
Kristopher Johnson
A: 

Hi,

thank you for your source code. I tried it and the only thing happend, is that my app crashes. Do you have an Idea how this could happen??

Greetings C

chris