ios - 在 iOS 中将 pdf 转换为 png 会导致外观质量大幅降低

标签 ios xcode pdf cgcontext

我正在将 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];

最佳答案

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

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

关于ios - 在 iOS 中将 pdf 转换为 png 会导致外观质量大幅降低,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15455151/

相关文章:

ios - 从 XCode 7 和 Swift 2.2 移动到 XCode 8 Swift 2.3 : frame size changed

ios - 使用 BannerViewController 时没有 UITabBarItem 图像

iphone - 修改此代码以支持另一种 View

ios - 更新 uitableview 标题上的文本

ios - 如何在 XCode 调试器中评估语句

java - 这是服务器或客户端上的 Google Endpoints 错误吗?

html - 尝试在 iframe 中打开 .pdf 文件

java - PDFBox 设置覆盖特定页面

php - 用 PHP 在 PDF 模板文档上书写/绘图

ios - UITextField 像 safari 一样有进展