tags:

views:

286

answers:

1

What is the best way to get the size (width and height) of an NSBitmapImageRep?

This is what I'm doing now:

NSBitmapImageRep *bitmap;

NSInteger samplesPerPixel = [bitmap samplesPerPixel];
NSInteger bytesPerPlane = [bitmap bytesPerPlane];
NSInteger bytesPerRow = [bitmap bytesPerRow];
NSInteger numberOfPlanes = [bitmap numberOfPlanes];

NSUInteger numPixels = numberOfPlanes * bytesPerPlane / samplesPerPixel;
NSUInteger width = bytesPerRow / samplesPerPixel;
NSUInteger height = numPixels / width;

Seems like an awful lot of typing for something that should be as easy as [bitmap getWidth]...

I've seen some posts on the internet that query the size like this (this is not in the documentation):

NSSize imageSize = [bitmap size];

But the values don't seem to represent the pixel dimensions of the image (for example, I get a float value of 122.869835 instead of 256, which is the real pixel width of the image).

+3  A: 
Peter Hosey
@peter: where did you find this information? I looked and it's not in the documentation of NSBitmapImageRep.
lajos
Click on the links, and you'll see that these methods are (and their documentation is) in NSBitmapImageRep's superclass, NSImageRep.
Peter Hosey