使用 NSImageView 创建内存高效的缩略图 (cocoa/OSX)
我正在从大图像(通常为 512x512 甚至 4096 x 2048)创建小 NSImageView(32x32 像素),用于极端测试用例。我的问题是,在我的极端测试用例中,当我显示缩略图时,我的应用程序内存占用似乎增加了 15MB 以上,这让我认为 NSImage 是以 4096x2048 而不是 32x32 的形式存储在内存中,我想知道是否有是避免这种情况的一种方法。以下是我创建 NsImageView 的过程:
• 首先,我使用 initByReferencingFile 创建一个 NSImage:(指向 4096x2048 .png 文件)
• 接下来,我通过调用 initWithFrame 来初始化 NSImageView:
• 然后,我调用 setImage: 进行分配我的 NSImage 到 NSImageView
• 最后我将 NSImageView 设置为 NSScaleProportionally
我显然没有采取任何措施来强制 NSImage 缩小到32x32 但我很难找到一个好的方法来处理这个问题。
I am creating small NSImageViews (32x32 pixels) from large images often 512x512 or even 4096 x 2048 for an extreme test case. My problem is that with my extreme test case, my applicaiton memory footprint seems to go up by over 15MB when I display my thumbnail, this makes me think the NSImage is being stored in memory as a 4096x2048 instead of 32x32 and I was wondering if there is a way to avoid this. Here is the process I go through to create the NsImageView:
• First I create an NSImage using initByReferencingFile: (pointing to the 4096x2048 .png file)
• Next I initialize the NSImageView with a call to initWithFrame:
• Then I call setImage: to assign my NSImage to the NSImageView
• Finally I set the NSImageView to NSScaleProportionally
I clearly do nothing to force the NSImage to size down to 32x32 but I have had trouble finding a good way to handle this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需从原始图像创建一个新的 32x32 NSImage,然后释放原始图像即可。
首先,创建 32x32 图像:
然后,锁定图像上的焦点并在其上绘制原图:
然后您可以对较小的图像进行任意操作:
记住
释放
!You can simply create a new 32x32
NSImage
from the original and thenrelease
the original image.First, create the 32x32 image:
Then, lock focus on the image and draw the original on to it:
Then you may do as you please with the smaller image:
Remember to
release
!