我发现有时 NSImage 大小不是真实大小(对于某些图片),而 CIImage 大小始终是真实大小。我正在用这个进行测试image http://naturelap.files.wordpress.com/2010/02/mystic-nature.jpg.
这是我为测试而编写的源代码:
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
。您可以获得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];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)