获得正确的图像宽度和高度

时间:2022-11-19 13:06:21

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

我使用下面的代码来获取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:

但是有时候imageWidth, imageHeight不会返回正确的值。例如,当我读取图像时,EXIF信息显示:

PixelXDimension = 2272;
PixelYDimension = 1704;

But imageWidth, imageHeight outputs

但imageWidth imageHeight输出

521:390 

4 个解决方案

#1


8  

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.

NSImage size方法返回与屏幕分辨率有关的大小信息。要获得实际文件映像中表示的大小,需要使用NSImageRep。

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

参考nsimage-size-not real-size-with-some-picture链接并获得帮助

#2


16  

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中。如果您的文件只包含一个图像,则如下所示:

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.

图像是NSImage,图像尺寸是像素的图像大小。

#3


3  

the direct API gives also the correct results

直接API也提供了正确的结果

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

#4


1  

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.

苹果公司使用基于DPI的点系统来将点映射到物理设备像素。不管EXIF怎么说,重要的是您的画布必须显示多少逻辑屏幕点。

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

iOS和OSX为您执行这个映射。您应该关心的唯一大小是从UIImage.size返回的大小

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

你自己也不能(不应该去关心)做映射到设备的像素,这就是为什么苹果这么做的原因。

#1


8  

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.

NSImage size方法返回与屏幕分辨率有关的大小信息。要获得实际文件映像中表示的大小,需要使用NSImageRep。

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

参考nsimage-size-not real-size-with-some-picture链接并获得帮助

#2


16  

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中。如果您的文件只包含一个图像,则如下所示:

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.

图像是NSImage,图像尺寸是像素的图像大小。

#3


3  

the direct API gives also the correct results

直接API也提供了正确的结果

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

#4


1  

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.

苹果公司使用基于DPI的点系统来将点映射到物理设备像素。不管EXIF怎么说,重要的是您的画布必须显示多少逻辑屏幕点。

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

iOS和OSX为您执行这个映射。您应该关心的唯一大小是从UIImage.size返回的大小

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

你自己也不能(不应该去关心)做映射到设备的像素,这就是为什么苹果这么做的原因。