You can manually create an image of the correct size and content and set its NSImageView image so that NSImageView does nothing.
NSImage *newImg = [self resizeImage:sourceImage size:newSize]; [aNSImageView setImage:newImg];
The following function resizes the image to the new size, keeping the aspect ratio. If the image is smaller than the new size, it is scaled and filled with a new frame. If the image is larger than the new size, it is reduced and filled with a new frame
- (NSImage*) resizeImage:(NSImage*)sourceImage size:(NSSize)size{ NSRect targetFrame = NSMakeRect(0, 0, size.width, size.height); NSImage* targetImage = [[NSImage alloc] initWithSize:size]; NSSize sourceSize = [sourceImage size]; float ratioH = size.height/ sourceSize.height; float ratioW = size.width / sourceSize.width; NSRect cropRect = NSZeroRect; if (ratioH >= ratioW) { cropRect.size.width = floor (size.width / ratioH); cropRect.size.height = sourceSize.height; } else { cropRect.size.width = sourceSize.width; cropRect.size.height = floor(size.height / ratioW); } cropRect.origin.x = floor( (sourceSize.width - cropRect.size.width)/2 ); cropRect.origin.y = floor( (sourceSize.height - cropRect.size.height)/2 ); [targetImage lockFocus]; [sourceImage drawInRect:targetFrame fromRect:cropRect
Shagru
source share