views:

159

answers:

2

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 :-)

+2  A: 

I'm not really sure why the image wouldn't be the right size, but you could try "correcting" the size to match one of its representations' sizes:

NSBitmapImageRep* aRep = [[theImage representations] objectAtIndex: 0];

NSSize correctedImageSize = NSMakeSize([aRep pixelsWide], [aRep pixelsHigh]);
[theImage setSize:correctedImageSize];

(Of course, maybe the rep is also the wrong size!)

andyvn22
Hi andyvn22. I tried this but unfortunately it didn't fix the issue. If I log the size using the above code, it showns correctly as 2505x930 in the above example, but now the image becomes BIGGER by an indeterminate factor (varying from image to image). After a couple more days of trawling the internet I came across another two reports of the same problem -- but no solutions unfortunately. Best guess was that it was controlled by the resolution of the image, its physical dimensions, and the resolution of the computer itself. However, I fixed this now but thanks a lot for your input :-)
Bender
p.s. Yeah, I think your hunch about the rep being the wrong size is also true. Not that I'm saying there's a fault there, of course. In my experience, anything that doesn't work right always comes down to my own coding errors in the final analysis.
Bender
+1  A: 

The size method returns the size in points, not pixels. The pixelsHigh and pixelsWide methods are what you're after.

grahamparks
Hi grahamparks. I tried these methods, and they do indeed *log* the same image size (in this case 2505x930) as shown in 'Get Info'. Unfortunately however, when I load the image using these methods the displayed size is enlarged by a factor of anything from about x0.5 to x4. Had this scaling always been in a *fixed* proportion I'd have been able to adjust my measurements taken from the images, but this seems to be dictated by the physical size of the original image in a way that I still don't understand. While I *have* now solved the problem (see above) thanks a lot for your input :-)
Bender