本文介绍了NSImage大小不是真实大小与一些图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I see that sometimes NSImage size is not real size (with some pictures) and CIImage size is always real. I was testing with this image.

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

This is source code which I wrote for testing:

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

输出为:

是??

推荐答案

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

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. You can get an NSImageRep from an NSImage using the representations method. Alternatively you can create a NSBitmapImageRep subclass instance directly like this:

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例如)。

The loop takes into account that some image formats may contain more than a single image (such as TIFFs for example).

您可以使用以下内容创建此大小的NSImage:

You can create an NSImage at this size by using the following:

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

这篇关于NSImage大小不是真实大小与一些图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 06:57