1

I've got a custom NSView which draws a chart in my app. I am generating a PDF which includes the image. In iOS I do this using code like this:

UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, 0.0);
[self drawRect:self.frame];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

In iOS, the displays are retina which means the image is very high resolution, however, I'm trying to do this in my Mac app now and the quality of the image is poor because non-retina Macs will generate a non-high res version of the image.

I would like to force my NSView to behave as if it was retina when I'm using it to generate an image. That way, when I put the image into my PDF, it'll be much higher resolution. Right now, it's very blurry and not attractive.

Kenny Wyland
  • 20,844
  • 26
  • 117
  • 229
  • where's the code for how you do it in OS X? – rocky Mar 17 '14 at 23:13
  • Without more context on your PDF generating (is exporting PDFs an end-user feature of your program, or are you just making screenshots for documentation?), I can't know if this tip might be useful, but check out HiDPI mode: http://stackoverflow.com/questions/12124576/how-to-simulate-a-retina-display-hidpi-mode-in-mac-os-x-10-8-mountain-lion-on – Josh Freeman Apr 27 '14 at 07:05
  • It's an end-user feature. My app generates some info based on inputs and can export all of the info in PDF format so they can keep/save/email/etc it. – Kenny Wyland May 02 '14 at 03:16

2 Answers2

0

Even a Retina bitmap will still be blurry and unattractive when scaled up enough. Assuming the view draws its contents in drawRect:, rather than trying to render the view into a PDF at a fixed resolution, a better approach is to draw directly into a PDF graphics context. This will produce a nice scalable PDF. The drawing code will need to be factored so it can be used for both the view’s drawRect: and the PDF.

Also, the iOS documentation states you should never call drawRect: yourself. Call renderInContext: on the view‘s layer, or use the newer drawViewHierarchyInRect:afterScreenUpdates:.

Douglas Hill
  • 1,537
  • 10
  • 14
0

You can call -[NSView dataWithPDFInsideRect:] to get PDF data from the drawing in a view. For example:

NSData* data = [someView dataWithPDFInsideRect:someView.bounds];
[data writeToFile:@"/tmp/foo.pdf" atomically:YES];

Any vector drawing (e.g. text, Bezier paths, etc.) that your view and its subviews do will end up as scalable vector graphics in the PDF.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154