1

I'm rendering PDF content into a UIView and I'm seeing that the text provided by the PDF is blurry when zoomed.

The way I'm rendering the text is as follows

CGSize size = CGSizeMake(96, 9); // These numbers come from the PDF
NSString* text = @"Text to render";
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
[text drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

This UIImage has the correct size and when I inspect it via XCode it is crisp.

When I call CGContextDrawImage below

CGRect widgetRect = CGRectMake(0,0,180,90);
CGContextDrawImage(mainContext, size, image.CGImage);

The result is blurry.

Notes:

  1. The mainContext above has it's origin lower left so that's why I render text into a separate context and draw an image.

  2. The UIView has a contentScaleFactor of 3 and the mainContext has a size to match that scale.

  3. I looked at CGContextDrawImage draws large images very blurry and it doesn't address my problem.

  4. I can't reproduce this problem outside my app in it's own.

This last part shows me that the problem is somewhere in the app code so I' hoping for hints and ideas about where to look in the rendering pipeline and how to debug.

EDIT: Updated the calll to CGContextDrawImage to use the correct size.

Konrad Piascik
  • 679
  • 5
  • 9
  • 1
    You're creating a 96x9 point context. On a 3x device, that's 288x27 pixels. Then you're drawing it into a 180x90 point rectangle. Assuming the same 3x scaling, that's 540x270 pixels. You're scaling the image up by a factor of 1.875 horizontally and a factor of **10** vertically. Core Graphics has to stretch your pixels, so it's not going to be sharp (unless you use nearest-neighbor scaling, in which case it will be sharp but blocky). – rob mayoff Oct 03 '18 at 05:14
  • @robmayoff I've updated the question with the correct call. I used the wrong size in drawing but I'm still seeing bad results. – Konrad Piascik Oct 03 '18 at 13:11

0 Answers0