简体   繁体   English

视网膜和非视网膜设备上的renderInContext

[英]renderInContext on retina and non-retina devices

I am creating a PDF by taking a screenshot of a UIView, this is currently working great on the iPad3 with the retina display, but when testing on other devices with lower resolution screens I am having problems with text resolution. 我通过拍摄UIView的截图来创建PDF,目前在带有视网膜显示屏的iPad3上工作得很好,但是当在其他具有较低分辨率屏幕的设备上进行测试时,我遇到文本分辨率问题。

Here is my code: 这是我的代码:

    //start a new page with default size and info
    //this can be changed later to include extra info.
    UIGraphicsBeginPDFPage();

    //render the view's layer into an image context
    //the last option specifies scale. If 0, it uses the devices scale.
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 2.0);

    CGContextRef context = UIGraphicsGetCurrentContext();
    [view.layer renderInContext:context];
    UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    //render the screenshot into the pdf page CGContext
    [screenShot drawInRect:view.bounds];

    //close the pdf context (saves the pdf to the NSData object)
    UIGraphicsEndPDFContext();

I have also tried to set the UIGraphicsBeginImageContextWithOptions scale to 2.0, but this gives no change. 我还尝试将UIGraphicsBeginImageContextWithOptions比例设置为2.0,但这没有任何变化。 How can I force a view on an iPad2 to render at 2x resolution? 如何强制iPad2上的视图以2倍的分辨率渲染?

Expected output: 预期产量:

Imgur

Actual output: 实际产量:

Imgur

I ended up fixing this by recursively setting the contentScaleFactor property of the parent view and its subviews to 2.0. 我最终通过递归设置父视图及其子视图的contentScaleFactor属性来修复此问题。

The UIImage was rendering at the correct resolution, but the layer wasn't when renderInContext was being called. UIImage以正确的分辨率渲染,但是当renderInContext时,该层不是。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM