Search code examples
memory-managementreleasensimageapplescript-objc

NSImage release() in AppleScriptObjC


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?


Solution

  • 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