【问题标题】:Converting pdf to png in iOS results in substantially lower appearance quality在 iOS 中将 pdf 转换为 png 会导致外观质量大大降低
【发布时间】:2013-03-16 21:55:40
【问题描述】:

我正在将 pdf 文件转换为 png 图像,因此我可以将文本添加到 png 然后再转换回 pdf。我正在使用下面的代码转换为 png。这导致外观质量显着下降。有什么办法可以防止或尽量减少质量损失?

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writeableDBPath = [documentsDirectory stringByAppendingPathComponent:@"sample.pdf"];

CFStringRef path;
CFURLRef url;

path = CFStringCreateWithCString (NULL, [writeableDBPath UTF8String], kCFStringEncodingUTF8);
url = CFURLCreateWithFileSystemPath (NULL, path, kCFURLPOSIXPathStyle, 0);

CGPDFDocumentRef myDocument;

myDocument = CGPDFDocumentCreateWithURL(url);

UIGraphicsBeginImageContext(CGSizeMake(612,792));
CGContextRef currentContext = UIGraphicsGetCurrentContext();

CGContextTranslateCTM(currentContext, 10, 792); //596,842 //640x960, 
CGContextScaleCTM(currentContext, 1.0, -1.0); // make sure the page is the right way up

CGPDFPageRef page = CGPDFDocumentGetPage (myDocument, 1); // first page of PDF is page 1 (not zero)
CGContextDrawPDFPage (currentContext, page);  // draws the page in the graphics context

UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

NSString* imagePath = [documentsDirectory stringByAppendingPathComponent: @"test.png"];
[UIImagePNGRepresentation(image) writeToFile: imagePath atomically:YES];

【问题讨论】:

    标签: ios xcode pdf cgcontext


    【解决方案1】:

    您可以尝试增加要渲染到的图像上下文的大小:

    UIGraphicsBeginImageContext(CGSizeMake(1224,1584));
    ...
    CGContextTranslateCTM(currentContext, 10, 1584);
    

    但你真的应该像这样使用PDF context:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writeableDBPath = [documentsDirectory stringByAppendingPathComponent:@"sample.pdf"];
    
    CFStringRef path;
    CFURLRef url;
    
    path = CFStringCreateWithCString (NULL, [writeableDBPath UTF8String], kCFStringEncodingUTF8);
    url = CFURLCreateWithFileSystemPath (NULL, path, kCFURLPOSIXPathStyle, 0);
    
    CGPDFDocumentRef myDocument;
    myDocument = CGPDFDocumentCreateWithURL(url);
    
    // Create URL for PDF file
    NSString *filename = @"sample-out.pdf";
    NSURL *outputURL = [NSURL fileURLWithPathComponents:[NSArray arrayWithObjects:documentsDirectory, filename, nil]];
    
    // Create PDF context
    CGContextRef pdfContext = CGPDFContextCreateWithURL((CFURLRef)outputURL, NULL, NULL);
    CGPDFContextBeginPage(pdfContext, NULL);
    UIGraphicsPushContext(pdfContext);
    
    // Flip coordinate system
    CGRect bounds = CGContextGetClipBoundingBox(pdfContext);
    CGContextScaleCTM(pdfContext, 1.0, -1.0);
    CGContextTranslateCTM(pdfContext, 0.0, -bounds.size.height);
    
    CGContextDrawPDFPage (UIGraphicsGetCurrentContext(), CGPDFDocumentGetPage (myDocument, 1));
    // Drawing commands
    [@"Hello World!" drawAtPoint:CGPointMake(0, 0) withFont:[UIFont boldSystemFontOfSize:72.0f]];
    // Clean up
    UIGraphicsPopContext();
    CGPDFContextEndPage(pdfContext);
    CGPDFContextClose(pdfContext);
    

    【讨论】:

    • 谢谢。我一直尝试使用 pdf 上下文添加文本,但我得出的结论是这是不可能的。我弄错了吗?
    • 是的,我已经看到并测试了它。如果您正在创建一个全新的 pdf,它可以工作,但我希望将文本添加到现有的 pdf 中。也许我做错了什么?
    • 在绘制文本之前,您是否将拥有的页面绘制到新页面? CGContextDrawPDFPage (UIGraphicsGetCurrentContext(), CGPDFDocumentGetPage (myDocument, 1)); 与来自myDocument = CGPDFDocumentCreateWithURL(url); 的文档
    • 好的。在绘制页面后和写入文本之前翻转坐标系。一切都很完美。我非常感谢你的帮助 Ric。
    猜你喜欢
    • 2016-07-04
    • 1970-01-01
    • 2015-11-23
    • 1970-01-01
    • 2017-06-11
    • 1970-01-01
    • 2021-11-25
    • 2020-04-21
    • 1970-01-01
    相关资源
    最近更新 更多