一些图片的NSImage大小不是真正的大小?

时间:2012-02-13 16:24:44

标签: macos cocoa core-graphics nsimage

我看到有时NSImage大小不是真正的大小(有些图片),而CIImage大小总是真实的。我正在使用此image进行测试。

这是我为测试编写的源代码:

NSImage *_imageNSImage = [[NSImage alloc]initWithContentsOfFile:@"<path to image>"];

NSSize _dimensions = [_imageNSImage size];

[_imageNSImage release];

NSLog(@"Width from CIImage: %f",_dimensions.width);
NSLog(@"Height from CIImage: %f",_dimensions.height);




NSURL *_myURL = [NSURL fileURLWithPath:@"<path to image>"];
CIImage *_imageCIImage = [CIImage imageWithContentsOfURL:_myURL];


NSRect _rectFromCIImage = [_imageCIImage extent];

NSLog(@"Width from CIImage: %f",_rectFromCIImage.size.width);
NSLog(@"Height from CIImage: %f",_rectFromCIImage.size.height);

输出是:

enter image description here

那怎么可能?也许我做错了什么?

4 个答案:

答案 0 :(得分:41)

NSImage size方法返回与屏幕分辨率相关的大小信息。要获得实际文件图像中显示的大小,您需要使用NSImageRep。您可以使用NSImageRep方法从NSImage获取representations。或者,您可以像这样直接创建NSBitmapImageRep子类实例:

NSArray * imageReps = [NSBitmapImageRep imageRepsWithContentsOfFile:@"<path to image>"];

NSInteger width = 0;
NSInteger height = 0;

for (NSImageRep * imageRep in imageReps) {
    if ([imageRep pixelsWide] > width) width = [imageRep pixelsWide];  
    if ([imageRep pixelsHigh] > height) height = [imageRep pixelsHigh];  
}

NSLog(@"Width from NSBitmapImageRep: %f",(CGFloat)width);
NSLog(@"Height from NSBitmapImageRep: %f",(CGFloat)height);

循环考虑到某些图像格式可能包含多个图像(例如TIFF)。

您可以使用以下方法创建此大小的NSImage:

NSImage * imageNSImage = [[NSImage alloc] initWithSize:NSMakeSize((CGFloat)width, (CGFloat)height)];
[imageNSImage addRepresentations:imageReps];

答案 1 :(得分:5)

NSImage大小方法以磅为单位返回大小。要获得以像素为单位表示的大小,您需要检查NSImage.representations属性,该属性包含具有pixelWide / pixelHigh属性和简单更改大小NSImage对象的NSImageRep对象数组:

@implementation ViewController {
    __weak IBOutlet NSImageView *imageView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do view setup here.

    NSImage *image = [[NSImage alloc] initWithContentsOfFile:@"/Users/username/test.jpg"];

    if (image.representations && image.representations.count > 0) {
        long lastSquare = 0, curSquare;
        NSImageRep *imageRep;
        for (imageRep in image.representations) {
            curSquare = imageRep.pixelsWide * imageRep.pixelsHigh;
            if (curSquare > lastSquare) {
                image.size = NSMakeSize(imageRep.pixelsWide, imageRep.pixelsHigh);
                lastSquare = curSquare;
            }
        }

        imageView.image = image;
        NSLog(@"%.0fx%.0f", image.size.width, image.size.height);
    }
}

@end

答案 2 :(得分:5)

感谢Zenopolis的原始ObjC代码,这里有一个非常简洁的Swift版本:

func sizeForImageAtURL(url: NSURL) -> CGSize? {
        guard let imageReps = NSBitmapImageRep.imageRepsWithContentsOfURL(url) else { return nil }
        return imageReps.reduce(CGSize.zero, combine: { (size: CGSize, rep: NSImageRep) -> CGSize in
            return CGSize(width: max(size.width, CGFloat(rep.pixelsWide)), height: max(size.height, CGFloat(rep.pixelsHigh)))
        })
    }

答案 3 :(得分:0)

如果您的文件只包含一张图片,则可以使用:

let rep = image.representations[0]
let imageSize = NSSize(width: rep.pixelsWide, height: rep.pixelsHigh)

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

在此处复制并更新:https://stackoverflow.com/a/13228091/3608824