使用 NSImageView 创建内存高效的缩略图 (cocoa/OSX)

发布于 2024-12-06 09:22:53 字数 518 浏览 4 评论 0原文

我正在从大图像(通常为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怀里藏娇 2024-12-13 09:22:53

您只需从原始图像创建一个新的 32x32 NSImage,然后释放原始图像即可。

首先,创建 32x32 图像:

NSImage *smallImage = [[NSImage alloc]initWithSize:NSMakeSize(32, 32)];

然后,锁定图像上的焦点并在其上绘制原图:

NSSize originalSize = [originalImage size];
NSRect fromRect = NSMakeRect(0, 0, originalSize.width, originalSize.height);
[smallImage lockFocus];
[originalImage drawInRect:NSMakeRect(0, 0, 32, 32) fromRect:fromRect operation:NSCompositeCopy fraction:1.0f];
[smallImage unlockFocus];

然后您可以对较小的图像进行任意操作:

[imageView setImage:smallImage];

记住释放

[originalImage release];
[smallImage release];

You can simply create a new 32x32 NSImage from the original and then release the original image.

First, create the 32x32 image:

NSImage *smallImage = [[NSImage alloc]initWithSize:NSMakeSize(32, 32)];

Then, lock focus on the image and draw the original on to it:

NSSize originalSize = [originalImage size];
NSRect fromRect = NSMakeRect(0, 0, originalSize.width, originalSize.height);
[smallImage lockFocus];
[originalImage drawInRect:NSMakeRect(0, 0, 32, 32) fromRect:fromRect operation:NSCompositeCopy fraction:1.0f];
[smallImage unlockFocus];

Then you may do as you please with the smaller image:

[imageView setImage:smallImage];

Remember to release!

[originalImage release];
[smallImage release];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文