通过渲染质量不佳的 iOS 从 UIView 生成 PDF
我有一个名为 TTT_WantsToBeRazorSharpView
的自定义 UIView。
这个 View 什么都不做,只是用
NSString*txtPleaseHelp = NSLocalizedString(@"Hello, am I blurry again?",@"");
CGContextShowTextAtPoint(ctx, 10, 50, [txtPleaseHelp cStringUsingEncoding:NSMacOSRomanStringEncoding], [txtPleaseHelp length]);
现在 View 被绘制三次到 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];
第一个代码绘制的 View 非常清晰,而第二个则不是,但没关系,因为 rect 的逗号值 (361.5, 251.5, 301.5, 80.5)。
看这张图:
但我现在的问题是,如果我将 View 渲染成 pdf 文档,它会变得模糊! 不知道为什么,请看这里:
和 PDF 文件本身 Test.pdf https://raw.github.com/florianbachmann/GeneratePDFfromUIViewButDontWantToLooseQuality/master/Test.pdf
以及将 View 渲染成 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
最佳答案
试试这个:
- (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,使文本也可以选择。
关于objective-c - iOS:从 UIView 生成 PDF 使文本模糊,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12563910/