Problem taking a UIImage of a view and attaching it to an email
My application just sends me an empty png (although it is the right size at least). The view I'm telling it to draw the image from definitely has content. Here is the relevant code.
UIGraphicsBeginImageContext(graph_window_view_controller.view.bounds.size);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * data = UIImagePNGRepresentation(image);
...
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
[mailViewController setSubject:[@"******** " stringByAppendingString:theTime]];
[mailViewController setMessageBody:@"*********" i开发者_如何学编程sHTML:NO];
[mailViewController setToRecipients:[NSArray arrayWithObject:str]];
[mailViewController addAttachmentData:data mimeType:@"image/png" fileName:@"chart_image.png"];
Can anyone spot any problems? Thanks.
You aren't actually drawing the view onto the context. You will have to use renderInContext:
method of CALayer
so add QuartzCore
framework to your project and #import <QuartzCore/QuartzCore.h>
.
Do
[graph_window_view_controller.view.layer renderInContext:UIGraphicsGetCurrentContext()];
before your UIGraphicsGetImageFromCurrentImageContext()
call.
精彩评论