获取正确的图像宽度和高度的NSImage [英] Get the correct image width and height of an NSImage
本文介绍了获取正确的图像宽度和高度的NSImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用下面的代码来获取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);
但是有时imageWidth
,imageHeight
不会返回正确的值.例如,当我阅读图像时,EXIF信息将显示:
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;
但是imageWidth,imageHeight输出
But imageWidth, imageHeight outputs
521:390
推荐答案
以像素为单位的图像尺寸存储在图像的NSImageRep中.如果您的文件仅包含一张图片,它将是这样的:
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);
其中 image 是您的NSImage, imageSize 是您的图像大小(以像素为单位).
where image is your NSImage and imageSize is your image size in pixels.
这篇关于获取正确的图像宽度和高度的NSImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文