1

I have an NSImageView in the lower left with a red background. In the screenshot it is displaying a PDF document. Screenshot

I want the shape of the document filled in white white, instead it is transparent and the red background bleeds through the document. How do I force the PDF to not be transparent?

Or the more general question, how do I get the NSImageView to fill transparent images with a solid color. My current strategy is to create a new image the same size with a white background and draw the original image on top and then use the new image in the image view. Is there a better way?

NSImage* newImage = [[NSImage alloc] initWithSize:[image size]];
[newImage lockFocus];
[[NSColor whiteColor] set];
CGRect rc = NSMakeRect(0,0,[newImage size].width, [newImage size].height);
NSRectFill(rc);
[image drawInRect:rc];
[newImage unlockFocus];
image = newImage;
Roland Rabien
  • 8,750
  • 7
  • 50
  • 67
  • Why not use PDFKit? That said it's probably not actually a white background in the document. Just viewers show white by default. It's probably a transparent or undefined background. – uchuugaka Dec 15 '14 at 04:45
  • Another solution is to do some view layering (not CALayer) or to simply draw into a custom view. That view can easily be a composition of subviews as well. The possibilities are endless – uchuugaka Dec 15 '14 at 04:48

0 Answers0