• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
How to detect a Retina Mac
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How to detect a Retina Mac


  • Subject: How to detect a Retina Mac
  • From: "Gerriet M. Denkmann" <email@hidden>
  • Date: Sun, 18 Aug 2013 20:03:43 +0700

I just noticed that the program I use to create Png files creates files with twice the number of pixels in each dimension.
Obviously because since I last used it, I have switched to a Retina Mac Book.

Ok, so I have to fix this program.

But: how do I detect that the programm is running on a computer where each pixel is really four pixels?
Both NSScreen and NSWindow have a backingScaleFactor property, which seems to be right.

But my code does neither involves a screen nor a window:

finalWidth = 50; 	because I want a Png 50 pixels wide
finalWidth /= 2;	← this fixes my Retina problem, but hardcoding this looks simply wrong
CGFloat scaleFactor = finalWidth / bounds.size.width;
NSSize sizE = NSMakeSize( finalWidth, bounds.size.height * scaleFactor );
NSImage *image = [[NSImage alloc] initWithSize: sizE];
[image setFlipped:YES];
[image lockFocus];
NSGraphicsContext *currentContext = [NSGraphicsContext currentContext];
NSAffineTransform *transform = [NSAffineTransform transform];
[transform scaleXBy: scaleFactor  yBy: scaleFactor ];
[transform translateXBy: -bounds.origin.x  yBy: -bounds.origin.y ];
[transform concat];

... do some drawing

[image unlockFocus];
NSRect recct = NSMakeRect(0, 0, sizE.width, sizE.height );
[image lockFocus];
NSBitmapImageRep *bitmapImageRep = [[NSBitmapImageRep alloc] initWithFocusedViewRect: recct ];
[image unlockFocus];
NSData *data = [ bitmapImageRep representationUsingType: NSPNGFileType  properties: nil ];

... write data to file

So where do I get my backingScaleFactor from?
What if there are two screens, one Retina, one not?

Kind regards,

Gerriet.


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden


  • Follow-Ups:
    • Re: How to detect a Retina Mac
      • From: Simone Tellini <email@hidden>
    • Re: How to detect a Retina Mac
      • From: Tom Davie <email@hidden>
  • Prev by Date: Re: predefined macro iOS vs OS X
  • Next by Date: Re: predefined macro iOS vs OS X
  • Previous by thread: UITableViewCell delete button disappearing when tapped
  • Next by thread: Re: How to detect a Retina Mac
  • Index(es):
    • Date
    • Thread