Kristopher Johnson
Kristopher Johnson

Reputation: 82545

Saving a View as a Photo in iPhone App

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

Upvotes: 2

Views: 3247

Answers (1)

Kristopher Johnson
Kristopher Johnson

Reputation: 82545

I added 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];
}

Upvotes: 15

Related Questions