Mik*_*ike 0 pdf xcode cgcontext ios
我正在将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];
Run Code Online (Sandbox Code Playgroud)
您可以尝试增加要渲染的图像上下文的大小:
UIGraphicsBeginImageContext(CGSizeMake(1224,1584));
...
CGContextTranslateCTM(currentContext, 10, 1584);
Run Code Online (Sandbox Code Playgroud)
但你真的应该使用这样的PDF上下文:
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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3128 次 |
| 最近记录: |