Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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. How can I force a view on an iPad2 to render at 2x resolution?

Expected output:

Imgur

Actual output:

Imgur

like image 616
danielbeard Avatar asked Nov 05 '12 03:11

danielbeard


1 Answers

I ended up fixing this by recursively setting the contentScaleFactor property of the parent view and its subviews to 2.0.

The UIImage was rendering at the correct resolution, but the layer wasn't when renderInContext was being called.

like image 115
danielbeard Avatar answered Oct 23 '22 14:10

danielbeard