NSImage release() in AppleScriptObjC

◇◆丶佛笑我妖孽 提交于 2019-12-11 04:29:16

问题


I'm creating a Table View with small images displayed in each row. The data source holds an image for each row of an table. I create images thru set img to NSImage()'s alloc()'s initWithContentsOfFile_(thePath). Next I display the image of selected row in bigger, browser view thru set imageView's image to img. Problem is that I'm running out of memory because I use the same representation for both views. (Images are around 500x400). I'm trying to figure out how to make a smaller version from bigger one and then release a bigger image.

I started from releasing an image.. I have tried img's release() but it not worked. I couldn't find any info on that topic besides Garbage Collection, ARC, and managing memory. I have turned-on Objective-C Automatic Reference Counting in XCode but probably it doesn't applies to Applescript part of a deal. Is there an way to achieve it in pure ApplescriptObjC?


回答1:


Try this code (adjusting for your variable names and UI control properties):

set curImage to current application's NSImage's alloc()'s initWithContentsOfFile:thePath

-- create a smaller image

set newImage to current application's NSImage's alloc()'s initWithSize:{48, 48}

newImage's lockFocus()

curImage's drawInRect:{{0,0}, {48, 48}} fromRect:{{0,0},{0,0}} operation:2 fraction:1.0

newImage's unlockFocus()

imgArtwork's setImage:newImage 

set curImage to missing value -- release large image


来源:https://stackoverflow.com/questions/38255556/nsimage-release-in-applescriptobjc

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