是否可以通过电子邮件发送UIView?

时间:2012-11-16 16:08:50

标签: objective-c ios email uiview drawing

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

2 个答案:

答案 0 :(得分: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];
}

答案 1 :(得分: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:,它接受​​三个参数。查看Apple文档以获取更多信息。