我想截取一个视图控制器的屏幕截图并将其加载到电子邮件中。我遇到的问题是我可以截取当前视图控制器的屏幕截图但我希望采用单独的视图控制器。
- (IBAction)showEmail:(id)sender
{
UIGraphicsBeginImageContext(self.view.frame.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *example = UIImageJPEGRepresentation(image, 1.0);
// Email Subject
NSString *emailTitle = @"Test";
// Email Content
NSString *messageBody = @"Attached below is the snapshot of the view controller";
// To address
NSArray *toRecipents = [NSArray arrayWithObject:@"testemail@hotmail.co.uk"];
// UIImage *UIImageToSend;
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:NO];
[mc setToRecipients:toRecipents];
[mc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
// NSData *data = UIImageJPEGRepresentation(UIImageToSend,1);
//UIImage *myImage = [UIImage imageNamed:@"example.png"];
//NSData *mageData = UIImagePNGRepresentation(myImage);
[mc addAttachmentData:example mimeType:@"image/png" fileName:@"example.png"];
// Present mail view controller on screen
[self presentViewController:mc animated:YES completion:NULL];
}
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(@"Mail cancelled");
break;
case MFMailComposeResultSaved:
NSLog(@"Mail saved");
break;
case MFMailComposeResultSent:
NSLog(@"Mail sent");
break;
case MFMailComposeResultFailed:
NSLog(@"Mail sent failure: %@", [error localizedDescription]);
break;
default:
break;
}
// Close the Mail Interface
[self dismissViewControllerAnimated:YES completion:NULL];
}
我试过改变自我
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
到另一个视图控制器类名,但xcode然后给我一个错误,说
在'otherViewController'类型的对象上找不到'view'的属性
答案 0 :(得分:1)
您保存jpeg数据但声明了mng类型的png。简单的不匹配:))