使用NSImageView(cocoa/OSX)创建内存有效的缩略图 [英] Creating memory efficient thumbnails using an NSImageView (cocoa/OSX)

查看:234
本文介绍了使用NSImageView(cocoa/OSX)创建内存有效的缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从大型图像创建小型NSImageViews(32x32像素),对于一个极端的测试用例,通常为512x512甚至4096 x 2048.我的问题是,在极端的测试用例中,显示缩略图时,我的应用程序内存占用似乎增加了15MB以上,这使我认为NSImage以4096x2048而不是32x32的形式存储在内存中,我想知道是否存在是避免这种情况的一种方法.这是我创建NsImageView的过程:

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:

•首先,我使用initByReferencingFile创建一个NSImage :(指向4096x2048 .png文件)

• First I create an NSImage using initByReferencingFile: (pointing to the 4096x2048 .png file)

•接下来,我通过调用initWithFrame初始化NSImageView:

• Next I initialize the NSImageView with a call to initWithFrame:

•然后我调用setImage:将NSImage分配给NSImageView

• Then I call setImage: to assign my NSImage to the NSImageView

•最后,我将NSImageView按比例设置为NSScale

• Finally I set the NSImageView to NSScaleProportionally

我显然没有采取任何措施强制将NSImage缩小至32x32,但是我很难找到一种好的方法来处理此问题.

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.

推荐答案

您可以简单地从原始图像创建一个新的32x32 NSImage,然后在原始图像中创建release.

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

首先,创建32x32图像:

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];

记住release

[originalImage release];
[smallImage release];

这篇关于使用NSImageView(cocoa/OSX)创建内存有效的缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆