arachide
arachide

Reputation: 8076

Get the correct image width and height of an NSImage

I use the code below to get the width and height of a NSImage:

NSImage *image = [[[NSImage alloc] initWithContentsOfFile:[NSString stringWithFormat:s]] autorelease];
imageWidth=[image size].width;
imageHeight=[image size].height;
NSLog(@"%f:%f",imageWidth,imageHeight);

But sometime imageWidth, imageHeight does not return the correct value. For example when I read an image, the EXIF info displays:

PixelXDimension = 2272;
PixelYDimension = 1704;

But imageWidth, imageHeight outputs

521:390 

Upvotes: 23

Views: 17601

Answers (6)

slboat
slboat

Reputation: 532

i make a extension like this:

extension NSImage{
    var pixelSize: NSSize?{
        if let rep = self.representations.first{
            let size = NSSize(width: rep.pixelsWide, height: rep.pixelsHigh)
            return size
        }
        return nil
    }
}

Upvotes: 2

Carter Cobb
Carter Cobb

Reputation: 840

SWIFT 4

You have to make a NSBitmapImageRep representation of the NSImage to get the correct pixel height and width. First a this extension to gather a CGImage from the NSImage:

extension NSImage {
    @objc var CGImage: CGImage? {
        get {
            guard let imageData = self.tiffRepresentation else { return nil }
            guard let sourceData = CGImageSourceCreateWithData(imageData as CFData, nil) else { return nil }
            return CGImageSourceCreateImageAtIndex(sourceData, 0, nil)
        }
    }
}

Then when you want to get the height and width:

let rep = NSBitmapImageRep(cgImage: (NSImage(named: "Your Image Name")?.CGImage)!)
let imageHeight = rep.size.height
let imageWidth = rep.size.width

Upvotes: 2

Karsten
Karsten

Reputation: 1939

the direct API gives also the correct results

CGImageRef cgImage = [oldImage CGImageForProposedRect:nil context:context hints:nil];
size_t width = CGImageGetWidth(cgImage);
size_t height = CGImageGetHeight(cgImage);

Upvotes: 4

Kibernetik
Kibernetik

Reputation: 3028

Dimensions of your image in pixels is stored in NSImageRep of your image. If your file contains only one image, it will be like this:

NSImageRep *rep = [[image representations] objectAtIndex:0];
NSSize imageSize = NSMakeSize(rep.pixelsWide, rep.pixelsHigh);

where image is your NSImage and imageSize is your image size in pixels.

Upvotes: 33

deleted_user
deleted_user

Reputation: 3805

Apple uses a point system based on DPI to map points to physical device pixels. It doesnt matter what the EXIF says, it matters how many logical screen points your canvas has to display the image.

iOS and OSX perform this mapping for you. The only size you should be concerned about is the size returned from UIImage.size

You cant (read shouldnt have to shouldnt care) do the mapping to device pixels yourself, thats why apple does it.

Upvotes: 2

Paresh Navadiya
Paresh Navadiya

Reputation: 38259

NSImage size method returns size information that is screen resolution dependent. To get the size represented in the actual file image you need to use an NSImageRep.

Refer nsimage-size-not-real-size-with-some-pictures link and get helped

Upvotes: 10

Related Questions