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
2009-01-16 06:35:51
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
2009-04-28 17:29:30
Without seeing your code, nobody can answer that. Kristophers code will render an image of any view you pass to it.
Roger Nolan
2009-04-28 19:24:07
If self.view isn't right, maybe it's a subview that you want to render?
Kristopher Johnson
2009-09-28 23:12:13
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
2009-09-28 19:21:30