Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

NSImage resizing on retina devices, saving my image into just double size. I want resize should not affected by device type.

I'm developing an app for Mac OS X.

I'm trying to resize any

NSImage
into a specific size like 200x300. It is working fine for Non-retina Mac.

But For Retina Mac, it is resizing the image as 400x600 which is just double, as we are expecting.

My project need is to resize the image as per given size regardless, the device on which application is running is retina or non-retina.

Is there any way to achive the target?

I have tried to measure the scale property, as for retina the scale is 2.0 so we are resizing the image for just half of the required.

But when we connected to monitors, one is retina and other is not retina, it again generating the same problem.

Here is the code which I have used for resizing:

-(void)resizeImage😟NSImage*)sourceImage newSize😟CGSize)newSize{    NSString*newImagePath =[[[Utility documentPath] stringByAppendingPathComponent:imagefolder] stringByAppendingPathComponent:@"imageName"];    [sourceImage setScalesWhenResized:YES];    [sourceImage setSize:newSize];    NSImage*newImage =[[NSImage alloc] initWithSize:newSize];    [newImage lockFocus];    NSRect frame =NSMakeRect(0,0, newSize.width, newSize.height);    [NSGraphicsContext saveGraphicsState];    NSBezierPath*path =[NSBezierPath bezierPathWithRoundedRect:frame xRadius:0 yRadius:0];    [path addClip];    [sourceImage drawInRect:frame fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];    [NSGraphicsContext restoreGraphicsState];    [newImage unlockFocus];    CGImageRefCGImage=[newImage CGImageForProposedRect:nil context:nil hints:nil];    NSBitmapImageRep*imgRep =[[[NSBitmapImageRep alloc] initWithCGImage:CGImage] autorelease];        NSData*data =[imgRep representationUsingType:NSPNGFileType properties: nil];        [[NSFileManager defaultManager] createFileAtPath:newImagePath contents:data attributes:nil];    [newImage release];}

Any help would be very helpful for me.

Thanks

iMac, Other OS, Mac OS X 10.8

Posted on Feb 18, 2013 6:22 AM

Reply
7 replies

Feb 18, 2013 6:43 AM in response to msuper69

Actually, What I have tried for resizing image as 200x300.


1. Test the application on Non Retina mac book:

Result: It was proper resizing as expected as 200x300.


2. Test the application on retina Macbook.

Result: The image was resizing as 400x600.


After analyzing the things, we get a result when we create CGImage after resizing it automatically doubles the size of our expected size.


3. So I have added a line of code to check the mac is retina or not:


CGFloat scalingFactor = [[self window] backingScaleFactor];

width = width/scalingFactor;


height = height/scalingFactor;


"My cocept was: for retina the scaling factor would be 2 so it will just half the width and hight before resizing, so we'll scale here for 100x150 for retina and 200x300 for non retina. And retina automatically doubles the size so in both case we get the same result as expected as 200x300"


Result: This solved my problem for Retina display also.


4. But When we use add another external monitor (Non Retina) with my retina display device. As the external monitor was non-retina but we were getting scalingFactor 2 because our main monitor is retina.


So here we again getting wierd result.

Feb 18, 2013 6:54 AM in response to nitin86

Reivew in the OS X documentation: User Experience\Windows & Views\NSWindow Class Reference\Instance Methods\backingScaleFactor


In particular:

Note: For almost all common cases, developers should avoid using the

backingScaleFactor
as an input to layout or drawing calculations. Developers should instead use the backing coordinate space conversion methods instead, as the resulting code will more likely work consistently and correctly under both standard and high-resolution operation.

NSImage resizing on retina devices, saving my image into just double size. I want resize should not affected by device type.

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.