NSImage 大小错误
我想我在这里遗漏了一些非常基本的东西。如果我使用我知道存在的合法 URL/路径执行此操作:
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,我还尝试检查第一个表示的大小,它返回了相同的混乱值。为什么图像无法正确加载?
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);
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
size
方法返回一个NSSize
,这是一个结构体,其width
和height
成员的类型为float< /代码>。您将它们视为
int
。使用%f
一切都会好起来的。The
size
method returns anNSSize
, a struct whosewidth
andheight
members are of typefloat
. You're treating them asint
. Use%f
and everything should be fine.这有帮助吗?
设置图像的宽度和高度。
讨论:
NSImage 对象的大小必须在使用之前设置。如果添加图像表示时尚未设置图像的大小,则从图像表示的数据中获取该大小。对于 EPS 图像,尺寸取自图像的边界框。对于 TIFF 图像,大小取自 ImageLength 和 ImageWidth 属性。
使用后更改 NSImage 的大小可以有效地调整图像的大小。更改大小会使其所有缓存失效并释放它们。当下次合成图像时,所选表示将在屏幕外窗口中绘制自身以重新创建缓存。
可用性
适用于 Mac OS X v10.0 及更高版本。
参见
Does this help?
Sets the width and height of the image.
Discussion:
The size of an NSImage object must be set before it can be used. If the size of the image hasn’t already been set when an image representation is added, the size is taken from the image representation's data. For EPS images, the size is taken from the image's bounding box. For TIFF images, the size is taken from the ImageLength and ImageWidth attributes.
Changing the size of an NSImage after it has been used effectively resizes the image. Changing the size invalidates all its caches and frees them. When the image is next composited, the selected representation will draw itself in an offscreen window to recreate the cache.
Availability
Available in Mac OS X v10.0 and later.
See Also
请参阅 NSImage size not real size with some pictures?
你需要迭代 NSImageRep 并从找到的最大值开始设置大小。
See NSImage size not real size with some pictures?
You need to iterate through NSImageRep and set the size from the largest found.