NSImage 加载图像的一部分
需求是这样的, 我会为按钮获取一个大的 PNG 图像,这个图像将包含需要显示的 hOver、按钮单击、鼠标退出的图像, 单个 PNG 文件大小为 1024 X 28,因此每个图像的大小约为 256 X 28, 我正在谷歌搜索最好的方法,但无法弄清楚如何实现这一点,
我想到了以下方法,
NSImage *pBtnImage[MAX_BUTTON_IMAGES]
for ( i = 0; i < 4 ; i++) {
pBtnImage[i] = [[NSImage alloc]initWithData:??????];
}
我想知道我应该在 NSData 参数中给出什么,
是否可以加载单个图像并相应地剪切图像当需要时。
提前致谢
The requirement is like this,
I would get a single large PNG Images for a button, this single image will contain images for hOver, button clicked , mouse exit that need to be displayed,
Single PNG File size would be 1024 X 28, so each image have size about 256 X 28,
I am googling the best possible approach but couldn't make out how to achieve this,
I have following approach in mind,
NSImage *pBtnImage[MAX_BUTTON_IMAGES]
for ( i = 0; i < 4 ; i++) {
pBtnImage[i] = [[NSImage alloc]initWithData:??????];
}
I want to know what should i give in the NSData parameter,
Is it possible to load a Single Image and clipped image accordingly as and when it needed.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有 Cocoa 支持的简单方法可以从图像的数据中仅读取图像的子矩形。然而,读取整个图像并在合成时仅使用图像的选定矩形是一件简单的事情。事实是,对于所有可用的 API,您可能最好只使用标准的 +[NSImage imageNamed:] 方法来单独读取图像并让操作系统处理缓存。
您想要解决什么实际的、可测量的性能问题?是否真的存在,或者这是一种过早优化的情况?
There's no simple Cocoa-supported way to read only a sub-rectangle of the image from its data. It's a simple matter, however, to read the whole image in and only use a select rectangle of the image when compositing. Thing is, with all the available API, you might be better off just to use the standard +[NSImage imageNamed:] method to read the images in individually and let the OS handle caching.
What actual, measured performance problem are you trying to solve? Does one really exist, or is this a case of premature optimization?