使用 NSImageView 和自定义 NSView 创建缩略图
我需要在我的 cocoa 应用程序 (OS X) 中执行以下操作:
- 允许用户将图像拖放到
NSImageView
上。 - 放置图像后,会在图像上投影一个固定大小的矩形,指示将用作缩略图的图像区域。
- 允许用户拖动矩形以精确定位所需的缩略图。
- 定位后,用户单击一个按钮即可将图像和缩略图保存到文件中。
这是我想要一些反馈的第二个要点。我的想法是执行以下操作:
- 在 IB 中,在我的视图上放置一个
NSImageView
,它将接受图像。 - 放置完成后,创建另一个
NSView
(与NSImageView
大小完全相同)并将其投影到NSImageView
上并显示矩形缩略图。我可能必须对 NSView 进行子类化,使其透明并支持矩形的绘制和重新定位。 - 允许用户拖动矩形。用户接受缩略图位置后,通过将矩形投影到 NSImageView 中的图像上来创建图像缩略图。
我知道要处理重叠视图,我需要为父视图打开图层 (setWantsLayer: YES
)。然而,我发现了一些关于此的混合报告,指出无法完成 NSView 的重叠实例,只能使用 CALayer 来完成。此外,如果有更简单的方法来捕获 NSImageView
的缩略图,我们将不胜感激。
I need to do the following in my cocoa app (OS X):
- Allow the user to drop an image on an
NSImageView
. - Once the image is dropped project a fixed size rectangle over the image, indicating the area of the image that will be used as a thumbnail.
- Allow the user to drag the rectangle around to pinpoint the required thumbnail.
- Once positioned, the user clicks a button which saves images and thumbnail to files.
It is the second bullet that I would like some feedback on. My idea is to do the following:
- In IB drop an
NSImageView
on my view which will accept the image. - Once the drop is done, create another
NSView
(exact same size as theNSImageView
) and project it over theNSImageView
and display the rectangle for the thumbnail. I will probably have to subclassNSView
so it is transparent and supports drawing and repositioning of the rectangle. - Allow the user to drag the rectangle around. Once the user accepts the thumbnail position, create the thumbnail from the image by projecting the rectangle on the image in
NSImageView
.
I understand that to work with overlapping views I need to turn on layers (setWantsLayer: YES
) for the parent view. However, I found some mixed reports on this, stating that overlapping instances of NSView
can't be done and this can only be done using CALayer
. Also if there are easier ways to capture a thumbnail off an NSImageView
, input is much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论