Image in image view

Hi All,

I am trying to now add an image to my image view, loaded from file.

At top level:


property NSImage : class "NSImage"

then in my load image handler:



set newImage to NSImages's alloc()'s initWithContentsOfFile_(this_file)
					imageField's setImage_(newImage)


no luck yet and I forget what syntax to use here for “newImage release.”

Any help appreciated.

Rob D

I have been trying to keep up with all the threads on ASOC and may have missed this:"

Is ASOC not Garbage Collected automatically?

All the best

Terry

I know Craig released the datasource in his booklist example but i don’t know about Images. The main issue is the image won’t load.

Rob

I have succeeded in placing an image into an NSView and I had difficulty with initWithContentsOfFile.

The path was the issue as it needs an NSString.

I eventually succeeded with this:

tell class "NSString" of current application to set imagePath to its stringWithString_("/Users/terry/Desktop/Devon/DSCF0534.jpg")

tell class "NSImage" of current application to set myImage to its alloc
		
tell myImage to initWithContentsOfFile_(imagePath) -- myImage is NSImage
		
my setNeedsDisplay_(true) -- my is the NSView


Does this help at all?

All the best

Terry

This is correct. Check that your file path is correct. It must be a POSIX style path.
To release you call the instantiated’s release method. This is the same for all objects.


newImage's release()

I have never used Garbage collection so you will have to read up on that to see
exactly how to use it. There are some caveats, but I do not remember what they are.

Can you confirm that Garbage Collection is NOT turned on by default.

From the little bit of Cocoa/Objective-C I know I think it can be on and any release calls in your code are ignored.

I think we need to know is it on or off by default?

All the best

Terry

Like I said, I have never used Garbage collection but I did find this article on Theococoa

Regards,

Craig

hi All,

I could not get this to work at all:


set newImage to NSImages's alloc()'s initWithContentsOfFile_(this_file)
                   imageField's setImage_(newImage)

or this


tell class "NSString" of current application to set imagePath to its stringWithString_(this_file)
				tell class "NSImage" of current application to set myImage to its alloc
				
				tell myImage to initWithContentsOfFile_(imagePath) -- myImage is NSImage
				theimageField's setImage_(myImage)
				myImage's release()

but this does work:



tell class "NSString" of current application to set imagePath to its stringWithString_(this_file)
				tell class "NSImage" of current application to set myImage to its alloc
				
				tell myImage to initWithContentsOfFile_(imagePath) -- myImage is NSImage
				theimageField's setImage_(myImage)
				myImage's release()


I had to create an instance of NSString at top too:

property NSString : class "NSString

"

Rob

Rob

Hi Rob,

Use your first example and put this at the top.

property NSString : class "NSString of current application

Hi Craig,
It seems like this simpler approach should work but

property NSString : class "NSString" of current application -- at top

				set newImage to NSImages's alloc()'s initWithContentsOfFile_(this_file)
				theimageField's setImage_(newImage)
				newImage's release()

this doesn’t.

I tried adding something like:


set imagePath to NSString's stringWithString_(this_file)
set newImage to NSImages's alloc()'s initWithContentsOfFile_(this_file)
				theimageField's setImage_(newImage)
				newImage's release()

I thought it was the stringWithString_(this_file) that got this_file into the right form for NSImages but can’t find a simple way to get it to work other than the loner approach in my last post.

Rob

First, I just noticed that you are using “NSImages’s” which has an extra “s.”

Second, no, it is not NSString that creates an image. It is the NSImage class.

You can set the property at the top for NSImage.

property NSImage : class "NSImage" of current application

and then call it:

set newImage to NSImage's alloc()'s initWithContentsOfFile_(this_file)

or you can call it like this without the property at the top.


tell class "NSImage" of current application
  set newImage to its alloc()'s initWithContentsOfFile_(this_file)
end tell

I made a simple app you can download here.

Thanks ever so much Craig,

I have to really get my syntax straight. I just noticed that extra 's after my last post but had all the other code in there and didn’t think to try the original line sans the extra “s”

That sorts it simply and elegantly. I have now completely converted my app over to the new form and it seems so much more direct and fast! Now the old applescript bridge to OBJ-c seems convoluted and awkward.

One more thing left for this app - I have to save all my settings to user defaults. Since the UI elements are now bound to my properties (which is excellent) I can not bind them to Shared User defaults. There is always an external file (your booklist app) but I want to use the user plist. lI’ll start another topic on that when I get totally confused.

Rob

I forgot to ask…

I will assume we still need

newImage's release()

after the call - unless I hear otherwise.

Rob

It is on by default. You can check yourself by going to the Build tab in Info.

BTW, one thing that looks to be off by default is saving scripts in release builds as uneditable.

That’s excellent.

I thought this should be the way in ASOC as a lot of users would not be interested in “retain” and “release”.

Thanks

Terry

Does that mean no "Release’ or “retain” is ever necessary?

Rob

That’s what garbage collection means, so I guess so. We don’t seem to have any access to the CF classes, so we don’t have them to worry about either.