A Composite Image problem

I am trying to fill an image view with an image formed by compositing a letter image with a number image. I wrote this little test program to troubleshoot what was going wrong with a larger program I wrote:

script CompositTestAppDelegate
	property parent : class "NSObject"
	property NSImage : class "NSImage"
	property Lview : missing value
	property Rview : missing value
	property myButton : missing value --attached to buttonPush
	
	on buttonPush_(sender)
		Rview's setImage_(composeImage_("A.tiff"))
	end buttonPush_
	
	on composeImage_(imageFile)
		set myLetter to NSImage's imageNamed_(imageFile)
		set myNumber to NSImage's imageNamed_("_2.tiff")
		myLetter's lockFocus()
		myNumber's drawAtPoint_fromRect_operation_fraction_({0, 0}, {{0, 0}, {0, 0}}, 11, 1)
		myLetter's unlockFocus()
		return myLetter
	end composeImage_
	
	on applicationWillFinishLaunching_(aNotification)
		set letterA to NSImage's imageNamed_("A.tiff")
		set number2 to NSImage's imageNamed_("_2.tiff")
		Lview's setImage_(letterA)
		--Rview's setImage_(composeImage_("A.tiff"))
	end applicationWillFinishLaunching_

As written, when the program runs, a letter A is put in Lview, and when I click on myButton a composite image “A2” appears in Lview --this is what I want to happen.

However, if I uncomment the last line in applicationWill FinishLaunching, then both views end up with “A2” in them when the program starts up. I then wanted to see if “A” went into Lview first and then converted to “A2” when the last line ran, so I put a “delay 1” in between the last 2 lines and it reverted to the correct behavior with “A” in Lview and “A2” in Rview. I’m totally baffled by this behavior.

My guess is that it’s something to do with caching. Look at the Cocoa Drawing Guide under Images and Caching.

Shane,
That was my guess too, since the image (in my real program) of a view is changing without anything in the code telling it to. It seems as though when the screen is refreshed, the image to redraw the views must be coming from some cached representation rather than the original file. But why the composited representation is being picked up instead of the original image is a mystery. I’ve tried setCacheMode_(3) (that’s NSImageCacheNever), but that didn’t help. I may just have to find another way to make the view I want instead of using a composite image.

Well you are modifying that image, if I read your code correctly.

Shane,

I was thinking originally that the compositing operation made a new image, but it does appear that it modifies the image I’m starting with (in memory at least, if not in the file). It looks like I can fix the problem by creating a new image object in the composeImage_ handler:

on composeImage_(imageFile)
		set x to the offset of "." in imageFile
		set letterPart to text 1 thru (x - 1) of imageFile
		set myBundle to NSBundle's mainBundle()
		set myPath to myBundle's pathForResource_ofType_(letterPart, "tiff")

		set myLetter to NSImage's alloc()'s initWithContentsOfFile_(myPath)

		set myNumber to NSImage's imageNamed_("_2.tiff")
		myLetter's lockFocus()
		myNumber's drawAtPoint_fromRect_operation_fraction_({0, 0}, {{0, 0}, {0, 0}}, 11, 1)
		myLetter's unlockFocus()
		return myLetter
	end composeImage_

This is working in the larger project I’m working on as well as this test program.

Right – you might make an NSImage by slurping up a file, but there’s no continuing link; it’s just a bunch of data in memory.