问题是获取视图的UIImage并将其附加到电子邮件中

时间:2011-07-18 19:57:15

标签: iphone ios xcode uiimage

我的应用程序只给我一个空的png(虽然它至少是正确的大小)。我告诉它从中绘制图像的视图肯定有内容。这是相关的代码。

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:@"*********" isHTML:NO];
[mailViewController setToRecipients:[NSArray arrayWithObject:str]];
[mailViewController addAttachmentData:data mimeType:@"image/png" fileName:@"chart_image.png"];

有人能发现任何问题吗?感谢。

1 个答案:

答案 0 :(得分:0)

您实际上并未将视图绘制到上下文中。您必须使用renderInContext: CALayer方法,因此请将QuartzCore框架添加到您的项目中,并#import <QuartzCore/QuartzCore.h>

待办事项

[graph_window_view_controller.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIGraphicsGetImageFromCurrentImageContext()来电之前