4

我的应用程序在 上绘制UIView,我想通过电子邮件发送此图。这可能吗?

4

2 回答 2

6

将其转换为图像并将该图像作为附件邮寄。

+ (UIImage *) imageWithView:(UIView *)view
{
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, [[UIScreen mainScreen] scale]);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

-(void)displayComposerSheet 
{
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;
    [picker setSubject:@"Check out this image!"];

    // Set up recipients
    // NSArray *toRecipients = [NSArray arrayWithObject:@"first@example.com"]; 
    // NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil]; 
    // NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@example.com"]; 

    // [picker setToRecipients:toRecipients];
    // [picker setCcRecipients:ccRecipients];   
    // [picker setBccRecipients:bccRecipients];

    // Attach an image to the email
    UIImage *coolImage = ...;
    NSData *myData = UIImagePNGRepresentation(coolImage);
    [picker addAttachmentData:myData mimeType:@"image/png" fileName:@"coolImage.png"];

    // Fill out the email body text
    NSString *emailBody = @"My cool image is attached";
    [picker setMessageBody:emailBody isHTML:NO];
    [self presentModalViewController:picker animated:YES];

    [picker release];
}
于 2012-11-16T16:11:33.987 回答
3

只有将其转换为图像时才能执行此操作。

转换为图像

您必须首先链接 QuartzCore 框架,并且#import <QuartzCore/QuartzCore.h>

接下来插入代码:

UIGraphicsBeginImageContext(myView.bounds.size);
[myView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

来源:http: //iphonedevelopment.blogspot.com/2008/10/getting-contents-of-uiview-as-uiimage.html

以电子邮件形式发送

您可以使用 MFMailComposeViewController 类,这样您就不必离开您的应用程序。本教程帮助了我:

http://iphonedevsdk.com/forum/tutorial-discussion/43633-quick-tutorial-how-add-mfmailcomposeviewcontroller.html

要添加图像,您可以使用同一个类的方法:addAttachmentData:mimeType:fileName:接受三个参数。查看苹果文档以获取更多信息。

于 2012-11-16T16:13:47.743 回答