Save image in image view?

From a lot of searching it doesn’t appear possible to simply save an image in an image view to a file.

I tried both passing the image to “Image Events” and also tried passing it to a cocoa method (not very good at cocoa, just a stab in the dark). The first returned an error from image events and the cocoa method also returned an unspecified error.

What am I doing wrong?

set theImage to image of image view "rss_pic" of tab view item "news" of tab view "maintabs" of window "prefs"
set image_path to ("/tmp/test.png") as Unicode text
-- this doesn't work
tell application "Image Events"
	-- tried maybe "setting" the image inside the tell block; before just "save theImage"... ; that did not work
        set IE_image to theImage
	save IE_image as PNG in timage without icon
		close IE_image
	end try
end tell
-- custom class defined earlier with methods introduced by Jobu
call method "savePNG:WithPath:" of CustomClass with parameters {theImage, image_path}

and here is the C method:

-(void)savePNG:(NSImage *)myImage WithPath:(NSString *)PNGPath {
//NSLog(@"Trying to save passed image to passed path");
 // this part works in another context ;  so problem is passing the image itself
NSImageRep *rep = [myImage bestRepresentationForDevice: nil];
NSSize newSize = { [rep pixelsWide], [rep pixelsHigh] };
[rep setSize: newSize];
NSData *thedata = [NSBitmapImageRep representationOfImageRepsInArray: [NSArray arrayWithObject:rep] usingType:NSPNGFileType properties:[NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:1.0] forKey:NSImageCompressionFactor]];
// end known to work
[thedata writeToFile:PNGPath atomically:NO];	
[myImage release];

PS: If I knew how to get an NSImage out of a specific image view entirely in cocoa I would maybe try that, instead of trying to pass the image to the cocoa method.

The same question is asked here without an answer:

Well… lucky me… I figured it out :wink:

Not sure exactly why, but:

  1. getting a reference to the image rather than the image seemed to work
  2. trying to release the image ([myImage release]:wink: in the c method caused a crash, so I eliminated it. hopefully that doesn’t leave any leaks… (!! ?)

set theImage to a reference to (image of image view "rss_pic" of tab view item "news" of tab view "maintabs" of window "prefs")
set image_path to ("/tmp/test.png") as Unicode text
call method "savePNG:WithPath:" of CustomClass with parameters {theImage, image_path}

I’ll let the macosxhints thread know…

still would like to know of a cleaner way…

PS: much of the code posted in the c method is not really necessary for this purpose. It really is designed to make sure high resolution images are appropriately sized. But since it was a working way to convert any image to PNG, I just kept it. point is, it could be cleaned up.

Thank you. That solved a problem I was having. I was trying to get the image from an image view and set it to an image cell in a table view… but I couldn’t… but using a reference to the image in the image view worked! I’m still not clear why it works but it does… so thanks!

Hi Woodenbrain,

two notes:

your Image Events code cannot work, because you have to open the picture with Image Events.
And you need the real path, Image Events doesn’t recognize the AppleScript Studio reference

The release message in the Obj-C code is wrong, because the method isn’t responsible for releasing the image

Thanks. On the first point, no doubt you are right. Too bad Image Events can’t get an image object in addition to a path though. On the second I mentioned that already in the second post. I did not edit it out of the first post intentionally so that the process at arriving at the solution is clear. Cheers.

I read also your second post

It doesn’t :slight_smile: