NSImage大小错误 [英] NSImage size is wrong
问题描述
我想我错过了一些基本的东西。如果我这样做,我知道存在一个合法的URL /路径:
I think i'm missing something really basic here. If I do this with a legal URL/path which I know exists:
NSImage* img = [[NSImage alloc] initWithContentsOfFile:[[selectedItem url] path]];
NSLog(@"Image width: %d height: %d", [img size].width, [img size].height);
然后我得到控制台报告宽度是-2080177216和高度0.虽然我知道宽度实际上是50和高度50.我试图调用isValid并返回YES,我也尝试检查第一个表示的大小,它返回了相同的混乱的值。
then I get reported to the console that the width is -2080177216 and the height 0. Although I know that the width is actually 50 and the height 50. I tried calling isValid and it returns YES, and I also tried checking the size of the first representation and it returned the same messed up values. How come the image is not loading properly?
推荐答案
size
方法返回 NSSize
, width
和 height
是 float
类型。你将它们视为 int
。使用%f
,一切都应该很好。
The size
method returns an NSSize
, a struct whose width
and height
members are of type float
. You're treating them as int
. Use %f
and everything should be fine.
这篇关于NSImage大小错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!