添加[自我截图];作为邮件附件?

时间:2014-05-15 09:20:53

标签: ios objective-c email screenshot

我想将[self screenshot];添加到我的邮件功能中作为附件。如何在不将图像保存到照片库的情况下执行此操作?这是我在ViewController.m中的代码:

截图功能:

- (UIImage *)screenshot
{
UIImage *backgroundImage = [UIImage imageNamed:@"iphoneframe.png"];
UIGraphicsBeginImageContext(backgroundImage.size);
[backgroundImage drawInRect:CGRectMake(0, 0, backgroundImage.size.width, backgroundImage.size.height)];

[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];

[screenshot drawInRect:CGRectMake(backgroundImage.size.width - screenshot.size.width, backgroundImage.size.height - screenshot.size.height, screenshot.size.width, screenshot.size.height)];

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return image;
}

电子邮件功能:

- (void)sendMail
{
if ([MFMailComposeViewController canSendMail]){

MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];

    mailer.mailComposeDelegate = self;

    [mailer setSubject:@"Mein Punktestand bei KlickMich"];

    //Attachement Object
    UIImage *myImage = [UIImage imageNamed:@"image.jpg"];
    NSData *imageData = UIImagePNGRepresentation(myImage);
    [mailer addAttachmentData:imageData mimeType:@"image/jpg" fileName:@"image.jpg"];

    NSString *messageBody = [NSString stringWithFormat:@"Hey, habe gerade ganze %i Punkte innerhalb von nur 15 Sekunden in der KlickMich App erreicht. Kannst du es besser?<br /><br />-> <a href='http://appstore.com/KlickMich'>appstore.com/KlickMich</a>",count];

    [mailer setMessageBody:messageBody isHTML:YES];

[self presentViewController:mailer animated:YES completion:NULL];

}

[…]

忠实的你 罗宾

1 个答案:

答案 0 :(得分:-1)

当您截取屏幕截图时,您必须将图像保存在DocumentDirectory中,如下所示:

NSArray *path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *imagePath=[path objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager] ;

BOOL isDir ;
imagePath =[imagePath stringByAppendingPathComponent:@"MyImage"];
if(YES == [fileManager fileExistsAtPath:imagePath isDirectory:&isDir])
{

}
else
{
    [fileManager createDirectoryAtPath:imagePath withIntermediateDirectories:NO attributes:nil error:nil];
}

imagePath =[imagePath stringByAppendingPathComponent:@"Image.jpg"];
NSData *tempImageData=[NSData dataWithData:UIImagePNGRepresentation(viewImage)];
[tempImageData writeToFile:imagePath atomically:YES];

当您想要分享该图片时,您必须从DocumentDirectory&amp;分享它。

 NSFileManager *fileManager = [NSFileManager defaultManager] ;
 NSArray *path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *imagePath=[path objectAtIndex:0];
 imagePath =[imagePath stringByAppendingPathComponent:@"MyImage"];
 NSArray *temp=[fileManager contentsOfDirectoryAtPath:imagePath error:nil];
 NSString *tempImgPath = [imagePath stringByAppendingPathComponent:[temp objectAtIndex:0]];
 NSData *imgData = [NSData dataWithContentsOfFile:tempImgPath];

 MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
 mailer.mailComposeDelegate = self;
 [mailer setSubject:@"Mein Punktestand bei KlickMich"];
[mailer addAttachmentData:imgData mimeType:@"image/jpg" fileName:@"image.jpg"];

NSString *messageBody = [NSString stringWithFormat:@"Hey, habe gerade ganze %i Punkte innerhalb von nur 15 Sekunden in der KlickMich App erreicht. Kannst du es besser?<br /><br />-> <a href='http://appstore.com/KlickMich'>appstore.com/KlickMich</a>",count];

[mailer setMessageBody:messageBody isHTML:YES];
[self presentViewController:mailer animated:YES completion:NULL];