【问题标题】:Get the correct image width and height of an NSImage获取 NSImage 的正确图像宽度和高度
【发布时间】:2012-08-09 04:46:42
【问题描述】:

我使用下面的代码来获取 NSImage 的宽度和高度:

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

但有时imageWidthimageHeight 不会返回正确的值。例如,当我阅读图像时,EXIF 信息会显示:

PixelXDimension = 2272;
PixelYDimension = 1704;

但是 imageWidth, imageHeight 输出

521:390 

【问题讨论】:

    标签: objective-c macos cocoa nsimage


    【解决方案1】:

    图像的像素尺寸存储在图像的 NSImageRep 中。如果你的文件只包含一张图片,它会是这样的:

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

    image 是您的 NSImage,imageSize 是您的图片大小(以像素为单位)。

    【讨论】:

      【解决方案2】:

      NSImage size 方法返回取决于屏幕分辨率的大小信息。要获得实际文件图像中表示的大小,您需要使用 NSImageRep。

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

      【讨论】:

      • Hii Price,in iOS 如何获取取决于屏幕分辨率的尺寸信息?你有什么想法吗?那么请帮助我。
      【解决方案3】:

      直接 API 也给出了正确的结果

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

      【讨论】:

      • 这似乎是在 Retina 屏幕上运行时唯一正确的答案。 NSImage 谎言(它返回的内容可能取决于图像数据声明的 DPI),NSImageRep 谎言(在 Retina 上它将返回真实像素大小的 2 倍);只有 CGImage 以像素为单位给出图像的实际大小。在 macOS 11 上,我无法找到任何其他方式来获取该信息。
      【解决方案4】:

      Apple 使用基于 DPI 的点系统将点映射到物理设备像素。 EXIF 说什么并不重要,重要的是您的画布必须有多少个逻辑屏幕点才能显示图像。

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

      你不能(阅读不应该不应该关心)自己映射到设备像素,这就是苹果这样做的原因。

      【讨论】:

        【解决方案5】:

        SWIFT 4

        您必须对NSImage 进行NSBitmapImageRep 表示,以获得正确的像素高度和宽度。 首先是这个扩展从NSImage 中收集一个CGImage

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

        那么当你想得到高度和宽度时:

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

        【讨论】:

          【解决方案6】:

          我做了一个这样的扩展:

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

          【讨论】:

            猜你喜欢
            • 2010-11-18
            • 1970-01-01
            • 2015-08-16
            • 1970-01-01
            • 1970-01-01
            • 2011-03-01
            • 2017-05-23
            • 2011-11-26
            • 2011-01-02
            相关资源
            最近更新 更多