Iphone iOS-通过渲染从UIView生成PDF会降低质量

Iphone iOS-通过渲染从UIView生成PDF会降低质量,iphone,ios,ipad,pdf,render,Iphone,Ios,Ipad,Pdf,Render,我使用以下方法从UIView生成PDF。它们都创建了PDF,但质量下降: 方法1: @implementation UIView(PDFWritingAdditions) - (void)renderInPDFFile:(NSString*)path { CGRect mediaBox = self.bounds; CGContextRef ctx = CGPDFContextCreateWithURL((__bridge_retained CFURLRef)[NSURL fi

我使用以下方法从UIView生成PDF。它们都创建了PDF,但质量下降:

方法1:

@implementation UIView(PDFWritingAdditions)

- (void)renderInPDFFile:(NSString*)path
{
    CGRect mediaBox = self.bounds;
    CGContextRef ctx = CGPDFContextCreateWithURL((__bridge_retained CFURLRef)[NSURL fileURLWithPath:path], &mediaBox, NULL);
    CGPDFPageRef page;
    CGContextDrawPDFPage(ctx, page);

    CGPDFContextBeginPage(ctx, NULL);

    // Also tried following commented lines but no luck
    //    CGContextSetFlatness(ctx, 0.1);
    //    CGContextSetAllowsAntialiasing(ctx, YES);
    //    CGContextSetAllowsFontSmoothing(ctx, YES);
    //    CGContextSetInterpolationQuality(ctx, kCGInterpolationHigh);
    CGContextScaleCTM(ctx, 1, -1);
    CGContextTranslateCTM(ctx, 0, -mediaBox.size.height);
    [self.layer renderInContext:ctx];
    CGPDFContextEndPage(ctx);
    CFRelease(ctx);
}

@end
方法2:

- (void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename
{
    // Creates a mutable data object for updating with binary data, like a byte array
    NSMutableData *pdfData = [NSMutableData data];

    // Points the pdf converter to the mutable data object and to the UIView to be converted
    UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil);
    UIGraphicsBeginPDFPage();
    CGContextRef pdfContext = UIGraphicsGetCurrentContext();


    // draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData

    [aView.layer renderInContext:pdfContext];

    // remove PDF rendering context
    UIGraphicsEndPDFContext();

    // Retrieves the document directories from the iOS device
    NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

    NSString* documentDirectory = [documentDirectories objectAtIndex:0];
    NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];

    // instructs the mutable data object to write its context to a file on disk
    [pdfData writeToFile:documentDirectoryFilename atomically:YES];
    NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename);
}
我想我可能缺少pdf上下文的一些设置,不确定。此外,我还使用以下方法从UIView创建了png图像,其质量完全相同:

- (UIImage *)imageFromView:(UIView *)view
{
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();
    // Retrieves the document directories from the iOS device
    NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

    NSString* documentDirectory = [documentDirectories objectAtIndex:0];
    NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:@"pdf.png"];

    // instructs the mutable data object to write its context to a file on disk
    [UIImagePNGRepresentation(img) writeToFile:documentDirectoryFilename atomically:YES];
    NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename);
    return img;
}
有人能看出我做错了什么吗?
谢谢。

这是用视网膜显示器吗?它可能是一个内容销售的东西。看看

编辑 好的,现在您已经发布了前后图片,我看到您希望发票UIView在PDF中呈现为漂亮、高质量、可打印的矢量数据

我很确定[CALayer renderInContext]总是生成光栅数据,因此您最好手动构建PDF。如果你说的是税务发票,你真的希望布局随着下一次iOS更新而改变吗


但是,您可以,这不情愿地建议使用UIView作为PDF布局的模板系统。

查看您的示例图像,我不确定您的期望是否正确。。。预览窗口部分放大,而您正在将位图图像绘制到PDF中,因此放大时会看到“锯齿边”。如果您查看实际大小(从预览中的“视图”菜单或Cmd-0),那么您应该会看到与屏幕截图大致相同的图像


但是,您真正想要做的不仅仅是将位图UIView渲染到PDF上下文中,而是将CoreGraphics绘图和布局直接渲染到PDF上下文中。查看本教程中的示例。这将确保放大时文本和行保持清晰,并确保打印在纸上的产品质量。Quartz2D矢量图形在这里是您的朋友,因为位图永远无法获得高质量,至少如果不创建一个巨大的PDF文件…

编辑好的,这不是完全相同的问题,但我认为解决方案将是类似的

我认为这是一场骗局

请看我的回答:

当你问PDF的页面大小时,你得到的是宽度/高度 72便士。您可以尝试创建一个使用 比例变换。例如,如果要以300 dpi渲染,请添加 按
300.0/72.0
缩放的缩放变换

如果导出为TIFF,则可以封装最终PPI (300)生成的图像


不,这在正常显示中发生。我已经为pdf手动创建了视图[因为还没有找到解决方案],并使用UIView获取点和颜色作为参考[因此,如果我更改视图中的任何内容,我不必更改代码]。但我添加的原始图像链接是使用“imageFromView”方法和光栅图像生成的。因此,我的观点是“RenderContext”应该能够生成相同质量的pdf。我也试着从那张图片中创建pdf,但没有成功。同样的问题。您需要通过调用CGContextFillRect、CTFontDrawGlyphs等创建PDF。RenderContext将始终创建外观糟糕的屏幕分辨率光栅PDF。以下是原始视图链接:和PDF链接后的链接:可能的副本