• 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
Re: How to detect a Retina Mac
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to detect a Retina Mac


  • Subject: Re: How to detect a Retina Mac
  • From: "Gerriet M. Denkmann" <email@hidden>
  • Date: Mon, 19 Aug 2013 17:05:47 +0700

On 19 Aug 2013, at 14:17, Graham Cox <email@hidden> wrote:

>
> On 19/08/2013, at 9:01 AM, Gerriet M. Denkmann <email@hidden> wrote:
>
>> I seem to need a way to tell NSImage NOT to double the pixels for me, but I don't see any way to do this.
>> Like: [ image setBackingScaleFactor: 1 ] but no such method seems to exist.
>>
>>
>> So hardcoding the retina-ness of my current computer seems to be the only solution.
>
>
> Of course it isn't.
>
> Marcel's answer looks correct to me - forget NSImage, use NSBitmapImageRep instead (without adding it to an NSImage). Instead of -lockFocus, etc, just create a context using the bitmap rep and set it as the current context. Then you can draw using either high-level stuff that uses "the current context" or lower level CG... functions.
>
> The problem you're running into is that NSImage is trying to be too darn smart. Don't give it the chance, by not using it AT ALL.

Yes, this really the crux of the matter. Once I eliminated NSImage, everything started working.

Here is the final code (if there is anything wrong or not quite right, please feel free to critizise it):

#define USE_IMAGEx	//	Off. Works, but has Retina hard-coded.

#ifdef USE_IMAGE
	finalWidth /= 2;	//	hardcoded Retina Fix
#endif

CGFloat scaleFactor = finalWidth / bounds.size.width;
NSSize sizE = NSMakeSize( finalWidth, bounds.size.height * scaleFactor );

#ifdef		USE_IMAGE
	NSImage *image = [[NSImage alloc] initWithSize: sizE];
	[image setFlipped:YES];
	[image lockFocus];
#else	//	USE_BITMAP
	NSInteger width = (NSInteger)sizE.width;
	NSInteger height = (NSInteger)sizE.height;
	NSInteger bps = 8;
	NSInteger spp = 4;//	RGBA
	NSBitmapImageRep *imageRep =	[ [ NSBitmapImageRep alloc ]	initWithBitmapDataPlanes:	NULL
																	pixelsWide:					width
																	pixelsHigh:					height
																	bitsPerSample:				bps
																	samplesPerPixel:			spp
																	hasAlpha:					YES
																	isPlanar:					NO
																	colorSpaceName: NSCalibratedRGBColorSpace
																	bytesPerRow:				0
																	bitsPerPixel:				0
									];
	NSGraphicsContext *bitmapContext1 = [ NSGraphicsContext graphicsContextWithBitmapImageRep: imageRep ];
	void *graphicsPort = [ bitmapContext1 graphicsPort ];
	NSGraphicsContext *bitmapContext2 =	[ NSGraphicsContext graphicsContextWithGraphicsPort:	graphicsPort
															flipped: 							YES
										];
	[NSGraphicsContext setCurrentContext: bitmapContext2 ];
#endif	//	USE_IMAGE or BITMAP

NSGraphicsContext *currentContext = [NSGraphicsContext currentContext];
NSLog(@"%s currentContext: %@",__FUNCTION__, currentContext);//<NSSnapshotBitmapGraphicsContext: 0x1018c1e40>

NSAffineTransform *transform = [NSAffineTransform transform];

#ifdef 		USE_IMAGE
	[transform scaleXBy: scaleFactor  yBy: +scaleFactor ];
	[transform translateXBy: -NSMinX(bounds)  yBy: -NSMinY(bounds) ];
#else	//	USE_BITMAP
	//	no idea why these have to be different:
	[transform scaleXBy: scaleFactor  yBy: -scaleFactor ];
	[transform translateXBy: -NSMinX(bounds)  yBy: -NSMaxY(bounds) ];
#endif	//	USE_IMAGE or BITMAP

[transform concat];

NSUInteger graphicIndex = [graphics count];
while (graphicIndex-- > 0)
{
	SKTGraphic *graphic = graphics[graphicIndex];
	[currentContext saveGraphicsState];
	[NSBezierPath clipRect:[graphic drawingBounds]];
	[graphic drawContentsInView:nil isBeingCreateOrEdited:NO];
	[currentContext restoreGraphicsState];
};

#ifdef 		USE_IMAGE
	[image unlockFocus];

	NSRect recct = NSMakeRect(0, 0, sizE.width, sizE.height );
	[image lockFocus];
	NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc] initWithFocusedViewRect: recct ];
	[image unlockFocus];
#endif	//	USE_IMAGE

NSBitmapImageFileType exTyp = isTiff ? NSTIFFFileType : NSPNGFileType;
NSData *data = [ imageRep representationUsingType: exTyp  properties: nil ];

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: Graham Cox <email@hidden>
References: 
 >How to detect a Retina Mac (From: "Gerriet M. Denkmann" <email@hidden>)
 >Re: How to detect a Retina Mac (From: Tom Davie <email@hidden>)
 >Re: How to detect a Retina Mac (From: "Gerriet M. Denkmann" <email@hidden>)
 >Re: How to detect a Retina Mac (From: Tom Davie <email@hidden>)
 >Re: How to detect a Retina Mac (From: "Gerriet M. Denkmann" <email@hidden>)
 >Re: How to detect a Retina Mac (From: Marcel Weiher <email@hidden>)
 >Re: How to detect a Retina Mac (From: "Gerriet M. Denkmann" <email@hidden>)

  • Prev by Date: Re: How to detect a Retina Mac
  • Next by Date: Re: How to detect a Retina Mac
  • Previous by thread: Re: How to detect a Retina Mac
  • Next by thread: Re: How to detect a Retina Mac
  • Index(es):
    • Date
    • Thread