iOS:从UIView生成PDF使文本模糊

时间:2012-09-24 11:16:05

标签: objective-c ios pdf uiview

通过渲染looses quality iOS

从UIView生成PDF

我有一个名为TTT_WantsToBeRazorSharpView的自定义UIView。 此视图不执行任何操作,但使用

绘制文本
NSString*txtPleaseHelp = NSLocalizedString(@"Hello, am I blurry again?",@"");
CGContextShowTextAtPoint(ctx, 10, 50, [txtPleaseHelp cStringUsingEncoding:NSMacOSRomanStringEncoding], [txtPleaseHelp length]);

现在,视图被绘制三次到UIViewController(在IB中一次),在代码中用这些行绘制两次(与下面的图像比较):

 TTT_WantsToBeRazorSharpView *customViewSharp = [[TTT_WantsToBeRazorSharpView alloc] initWithFrame:CGRectMake(10,250, 300, 80)];
 [self.view addSubview:customViewSharp];

 TTT_WantsToBeRazorSharpView *customViewBlurryButIKnowWhy = [[TTT_WantsToBeRazorSharpView alloc] initWithFrame:CGRectMake(361.5, 251.5, 301.5, 80.5)];
 [self.view addSubview:customViewBlurryButIKnowWhy];

第一个代码绘制视图是razorsharp,而第二个不是,但是没关系,因为rect的逗号值(361.5,251.5,301.5,80.5)。

看到这张图: See this picture

但我现在的问题是,如果我将视图渲染成pdf文档,那就太模糊了! 而且不知道为什么,请看这里: blurry pdf

和PDF文件本身Test.pdf https://raw.github.com/florianbachmann/GeneratePDFfromUIViewButDontWantToLooseQuality/master/Test.pdf

以及将视图呈现为pdf的行:

//this is blurry, but why? it can't be the comma coordinates
CGRect customFrame1 = CGRectMake(10,50, 300, 80);
TTT_WantsToBeRazorSharpView *customViewSharp = [[TTT_WantsToBeRazorSharpView alloc] initWithFrame:customFrame1];
CGContextSaveGState(context);
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, (int)customFrame1.origin.x, (int)customFrame1.origin.y);
[customViewSharp.layer renderInContext:context];
CGContextRestoreGState(context);

那么为什么renderInContext:context文字模糊不清?

我感谢您的所有提示和帮助,我甚至为您的勇者做了一个GitHub项目(有源):https://github.com/florianbachmann/GeneratePDFfromUIViewButDontWantToLooseQuality

3 个答案:

答案 0 :(得分:1)

试试这个:

- (void)initValues {
    UIColor *gray = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.3f];
    CALayer *l = [self layer];
    l.masksToBounds = YES;
    l.cornerRadius = 10.0f;
    l.borderWidth = 3.0f;
    self.backgroundColor = gray;
    l.backgroundColor = gray.CGColor;
    l.borderColor = gray.CGColor;
}

- (void)drawRect:(CGRect)rect {
    NSLog(@"TTT_WantsToBeRazorSharpView drawRect[%3.2f,%3.2f][%3.2f,%3.2f]",rect.origin.x,rect.origin.y,rect.size.width,rect.size.height);

    // get the graphic context
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetShouldSmoothFonts(ctx,YES);

    CGColorRef colorWhite = CGColorRetain([UIColor redColor].CGColor);

    CGContextSetFillColorWithColor(ctx, colorWhite);

    CGContextSelectFont(ctx, "Helvetica-Bold", 24, kCGEncodingMacRoman);
    CGAffineTransform tranformer = CGAffineTransformMakeScale(1.0, -1.0);
    CGContextSetTextMatrix(ctx, tranformer);

    //why is this sucker blurry?
    NSString*txtPleaseHelp = NSLocalizedString(@"Hello, am I blurry again?",@"");
    CGContextShowTextAtPoint(ctx, 10, 50, [txtPleaseHelp cStringUsingEncoding:NSMacOSRomanStringEncoding], [txtPleaseHelp length]);
    CGColorRelease(colorWhite);
}

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {

    if (!layer.shouldRasterize && !CGRectIsEmpty(UIGraphicsGetPDFContextBounds())) {
        [self drawRect:self.bounds];
    }
    else {
        [super drawLayer:layer inContext:ctx];
    }
}

我不得不将文字颜色更改为红色以使其可见。因为首先写入文本,然后将透明灰色按钮放在其上,这将使白色文本消失。添加drawLayer方法时,不需要光栅化的所有内容都会被写为矢量到pdf,使文本也可以选择。

答案 1 :(得分:0)

视图中的文本在PDF中呈现为位图而不是矢量图形,这会导致模糊的外观。我的猜测是iOS渲染引擎在中间位图上绘制文本(我认为是为了颜色组合目的)然后在目标上下文中呈现这个位图。

答案 2 :(得分:0)

我使用以下命令修复模糊文本:

    self.frame = CGRectIntegral(self.frame);

文本模糊的原因是对齐不理想,即点坐标中的分数值,因此文本因过度抗锯齿而显得模糊。