如何将当前的VIew转换为高分辨率PDF到电子邮件

时间:2014-05-16 18:43:14

标签: ios objective-c email pdf xcode5

我目前有代码可以抓取当前视图的屏幕抓取(包含带有可编辑文本字段的PNG图像视图),但它创建的jpg是72ppi。

我原来的png是326ppi。

如何使用apx 326 ppi捕获或创建当前视图的PDF?

我读了很多其他帖子,但没有一个是为了这个目的。

任何帮助都会很棒。

这里是我现在使用的代码:

- (IBAction)SnapSend:(id)sender {


    UIGraphicsBeginImageContext(self.view.frame.size);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    NSData * imageData = UIImageJPEGRepresentation(image, 1.0);

    if ( [MFMailComposeViewController canSendMail] ) {
        MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
        mc.mailComposeDelegate = self;
        [mc addAttachmentData:imageData mimeType:@"image/jpeg" fileName:@"attachment.jpg"];


    }


    NSLog(@" please add image to my message");


    // Email Subject
    NSString *emailTitle = @"";
    // Email Content
    NSString *messageBody = @"";
    // To address
    NSArray *toRecipents = [NSArray arrayWithObject:@""];

    MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
    mc.mailComposeDelegate = self;
    [mc setSubject:emailTitle];
    [mc setMessageBody:messageBody isHTML:NO];
    [mc setToRecipients:toRecipents];
    [mc addAttachmentData:imageData mimeType:@"image/jpeg" fileName:@"attachment.jpg"];



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


@end

2 个答案:

答案 0 :(得分:0)

UIImageJPEGRepresentation不允许您指定其生成的JFIF数据中嵌入的ppi。如果你给它一个非视网膜图像(你正在做),它会在JFIF中放置72 dpi,如果你给它一个视网膜图像,则为144 dpi。

首先,您应将UIGraphicsBeginImageContext来电更改为UIGraphicsBeginImageContextWithOptions并指定比例为2.0以创建视网膜图像。

然后,您需要使用ImageIO框架来创建JFIF数据,并且需要MobileCoreServices框架来指定JFIF类型的常量。

因此,您在变量UIImage中有一个image。以下代码从该图像创建JFIF数据并设置326 ppi:

NSMutableData *mutableData = [NSMutableData data];
CGImageDestinationRef destination = CGImageDestinationCreateWithData(
    (__bridge CFMutableDataRef)mutableData, kUTTypeJPEG, 1, NULL);

NSDictionary *properties = @{
    (__bridge id)kCGImageDestinationLossyCompressionQuality: @0.5,
    (__bridge id)kCGImagePropertyDPIWidth: @326,
    (__bridge id)kCGImagePropertyDPIHeight: @326,
};
CGImageDestinationAddImage(destination, image.CGImage,
    (__bridge CFDictionaryRef)properties);
CGImageDestinationFinalize(destination);
CFRelease(destination);

生成的JFIF数据位于mutableData

答案 1 :(得分:0)

忘记dpi。 (或ppi)。

这是关键:

UIGraphicsBeginImageContext(self.view.frame.size);

该语句以x和y分辨率(分别为宽度和高度)设置图形上下文;

定义输出的绝对分辨率。 当你将因子增加4.23(粗略),即72 ppi到326的比例因子时,你必须相应地对齐你的self.view几何。

就是这么简单:如果你想要绘制这些像素数,那么你将不得不画出这个像素数。