围绕某些 NSRect 剪切 NSImage
我有一个 NSImage,里面有不同的图像。它内部图像的位置将始终保持不变,因此我需要指定一个矩形以在该矩形位于图像内部的位置处获取子图像。这怎么可能做到呢?
I have an NSImage with different images inside it. The positions of the images inside of it will stay the same all the time, so I need to specify a rectangle to get a subimage at the position of the rectangle is inside of the image. How could this be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么一张图像里面有多个图像?过早优化?
无论如何,当您 绘制图像,你传递的参数之一就是你想要的图像的部分。通常,您可以传递图像的边界或零矩形(两者含义相同),但如果需要,您可以传递子矩形。
如果目标矩形比源矩形大,它将按比例放大(不按比例);如果较小,则会按比例缩小(不按比例)。如果您不想进行任何缩放,请调整目标矩形的大小以匹配源矩形的大小。
如果要将绘图留给 NSImageView,创建所需大小的空 NSImage,然后绘制到其中,然后将该图像传递到图像视图。这可能会破坏您希望通过将这些全部混合到一个图像中而获得的任何性能优势。
您还可以创建 NSView、NSImageView 或 CALayer 的自定义子类,它具有 NSImage 属性和源矩形属性,并且仅绘制该部分。
Why are there multiple images inside of one image? Premature optimization?
In any case, when you draw the image, one of the parameters you pass is the section of the image you want. Normally, you pass either the bounds of the image or the zero rect (which both mean the same thing), but you can pass a subrectangle if you want.
If the destination rectangle is larger than the source rectangle, it will scale up (non-proportionally); if it's smaller, it will scale down (non-proportionally). Adjust the size of the destination rectangle to match that of the source rectangle if you don't want any scaling.
If you want to leave the drawing to an NSImageView, create an empty NSImage of the desired size, then draw into it, then pass that image to the image view. This probably destroys whatever performance advantage you'd hoped to gain by mashing these all together into one image.
You could also create a custom subclass of NSView, NSImageView, or CALayer that has a property for the NSImage and a property for the source rectangle, and draws only that section.
从 macOS 10.6 开始,这很容易。
此代码采用包含多个剪辑的主图像
并提取该母版的一部分以生成剪切图像。
提示:使用循环,布局 'N'
NSImageViews
(其中“N”是剪辑数量),
将每个剪辑提取到其中一个 imageView 中,
并弄清楚如何计算每个剪辑的 xPos、yPos 和剪辑大小 (clipRect)。
当您可以一次查看所有剪辑时会更容易。
This is easy as of macOS 10.6.
This code takes a master image containing multiple clips
and extracts part of that master to produce a clipped image.
Tip: using a loop, layout 'N'
NSImageViews
(where 'N' is the number of clips),
extract each clip into one of those imageViews,
and figure out how to compute the xPos, yPos, and clip size (clipRect) for each clip.
It's easier when you can see all of the clips at once.