【问题标题】:Add [self screenshot]; as email attachment?添加【自拍截图】;作为电子邮件附件?
【发布时间】:2014-05-15 09:20:52
【问题描述】:

我想将[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];

}

[…]

你的忠实
罗宾

【问题讨论】:

  • 您能否展示您已经尝试过但不起作用的方法并更详细地解释问题?您是否很难将创建的图像引用到 sendMail 方法中?等等。

标签: ios objective-c email screenshot


【解决方案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 检索该图像并分享它。

 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];

【讨论】:

    猜你喜欢
    • 2021-10-30
    • 1970-01-01
    • 2012-02-28
    • 1970-01-01
    • 1970-01-01
    • 2019-06-05
    • 2017-04-11
    • 2011-10-11
    • 2011-02-03
    相关资源
    最近更新 更多