Creating memory efficient thumbnails using an NSImageView (cocoa/OSX)

余生长醉 提交于 2019-12-06 07:28:17

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];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!