I am developing an application for Mac OS X. I am trying to resize any NSImage to a specific size, for example, 200x300. It works great for Non-retina Mac. But for Retina Mac, it changes the image size to 400x600, which, as we expected, is twice as large. My project should resize the image to fit the given size, regardless of which device the retina or the retina works on. How can I achieve the goal.
I tried to measure the scale property, because for the retina, the scale is 2.0, so we only resize the image to half what it needs.
But when we connected to the monitors, one of them is a retina, and the other is not a retina, it again causes the same problem.
Here is the code I used to resize:
-(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]; CGImageRef CGImage = [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]; }
Thanks in advance.
source share