Monday, December 18, 2017

#1 2009-09-13 06:02:01 am

robdut
Member
Registered: 2009-09-02
Posts: 301

Image in image view

Hi All,

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

At top level:

Applescript:


property NSImage : class "NSImage"

then in my load image handler:

Applescript:



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

Offline

 

#2 2009-09-13 07:03:59 am

tellboy
Member
Registered: 2005-06-23
Posts: 152

Re: Image in image view

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

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


Is ASOC not Garbage Collected automatically?


All the best


Terry

Offline

 

#3 2009-09-13 08:47:27 am

robdut
Member
Registered: 2009-09-02
Posts: 301

Re: Image in image view

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

Offline

 

#4 2009-09-13 09:09:02 am

tellboy
Member
Registered: 2005-06-23
Posts: 152

Re: Image in image view

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:

Applescript:

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


Filed under: NSString, NSImage

Offline

 

#5 2009-09-13 11:48:11 am

Craig Williams
Administrator
From:: Ft. Smith, AR
Registered: 2006-12-07
Posts: 888

Re: Image in image view

robdut wrote:

Applescript:



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


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.

Applescript:


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.

Offline

 

#6 2009-09-13 12:16:54 pm

tellboy
Member
Registered: 2005-06-23
Posts: 152

Re: Image in image view

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

Offline

 

#7 2009-09-13 02:02:47 pm

Craig Williams
Administrator
From:: Ft. Smith, AR
Registered: 2006-12-07
Posts: 888

Re: Image in image view

tellboy wrote:

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

Garbage Collection is off by default. You enable it by double-clicking a target in your project and changing the "Objective-C Garbage Collection" setting to "Required"


Regards,

Craig

Offline

 

#8 2009-09-13 03:40:52 pm

robdut
Member
Registered: 2009-09-02
Posts: 301

Re: Image in image view

hi All,

I could not get this to work at all:

Applescript:


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

or this

Applescript:


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:

Applescript:



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:

Applescript:

property NSString : class "NSString

"


Rob


Rob


Filed under: NSString, NSImage

Offline

 

#9 2009-09-13 03:43:20 pm

Craig Williams
Administrator
From:: Ft. Smith, AR
Registered: 2006-12-07
Posts: 888

Re: Image in image view

Hi Rob,

Use your first example and put this at the top.

Applescript:

property NSString : class "NSString of current application

Offline

 

#10 2009-09-13 07:42:01 pm

robdut
Member
Registered: 2009-09-02
Posts: 301

Re: Image in image view

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

Applescript:

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:

Applescript:


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

Offline

 

#11 2009-09-13 08:03:01 pm

Craig Williams
Administrator
From:: Ft. Smith, AR
Registered: 2006-12-07
Posts: 888

Re: Image in image view

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.

Applescript:

property NSImage : class "NSImage" of current application

and then call it:

Applescript:

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

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

Applescript:


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.
set_image.png


Filed under: NSImage

Offline

 

#12 2009-09-13 09:06:29 pm

robdut
Member
Registered: 2009-09-02
Posts: 301

Re: Image in image view

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

Offline

 

#13 2009-09-13 09:11:06 pm

robdut
Member
Registered: 2009-09-02
Posts: 301

Re: Image in image view

I forgot to ask...

I will assume we still need

Applescript:

newImage's release()

after the call - unless I hear otherwise.

Rob

Offline

 

#14 2009-09-15 06:10:16 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5200

Re: Image in image view

tellboy wrote:

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


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.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#15 2009-09-16 03:10:36 am

tellboy
Member
Registered: 2005-06-23
Posts: 152

Re: Image in image view

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

Offline

 

#16 2009-09-16 06:26:03 am

robdut
Member
Registered: 2009-09-02
Posts: 301

Re: Image in image view

Does that mean no "Release' or "retain" is ever necessary?


Rob

Offline

 

#17 2009-09-16 07:51:44 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5200

Re: Image in image view

robdut wrote:

Does that mean no "Release' or "retain" is ever necessary?


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.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)