I'm trying to load an image into my NSImageView/NSScrollView and display it at actual size, but the image mysteriously ends up getting displayed at about half-size. I thought at first it might be being reduced to fit into some kind of constraints of the frame etc., but soon realised this couldn't be right because if I physically enlarge the image size (in an image editing program) and then load it again, I find I can presumably load/display images as big as I want to.
The actual size of the image in question is only 2505 x 930, which I guess isn't a problem since I can double and quadruple this without any apparent problems (except of course, that they're all reduced by about 50% when displayed). The relevant part of my very straightforward code is:
- (IBAction)openSourceImage:(NSString*)aFilepath
{
// obtain image filepath passed from 'chooseFile'...
NSImage *theImage = [[NSImage alloc] initWithContentsOfFile:aFilepath];
if (theImage)
{
[theImageView setImage:theImage];
// resize imageView to fit image; causes the surrounding NSScrollView to adjust its scrollbars appropriately...
[theImageView setFrame:
NSMakeRect([theImageView frame].origin.x, [theImageView frame].origin.y, [theImage size].width, [theImage size].height)];
[theImageView scrollRectToVisible:
NSMakeRect([theImageView frame].origin.x, [theImageView frame].origin.y + [theImageView frame].size.height,1,1)];
[theImage release]; // we're done with 'theImage' we allocated, so release it
// display the window title from the filepath...
NSString *aFilename = [aFilepath lastPathComponent];
[[theImageView window] setTitle:aFilename];
}
}
Can anyone please tell me where I'm going wrong here, and how to display images at actual size?
Solution: Okay, so calling 'size' results in a displayed image that is too small to work with and calling 'pixelsHigh/pixelsWide' results in a magnified image of indeterminate scale...
My test app uses a couple of slider-driven 'crosshairs' to plot the coordinates of features on an image (like a photo or map for instance). By pure chance, I accidentally noticed that while the loaded image only displayed at a fraction of its actual size, the x,y coordinates DID correspond to real-life (indicating about 70 pixels/inch). Go figure that one...
Using:
[theImage setSize:NSMakeSize(imageSize.width * 4, imageSize.height * 4)];
I'm now able to load the image at a KNOWN magnification and reduce my plotted x,y measurements by the same factor. I also threw in an NSAffineTransform method to allow me to zoom in/out for the best viewing size.
Phew! That was challenging for someone at my fairly novice level, and I still don't understand the underlying cause of the original display problem, but I guess the end result is all that counts. Thanks again to both of you :-)