Load web image to NSImageView?

How do you load an image from the internet (assuming you know the URL) into an NSImageView? I know you could do it the long way, saving the image to a temp location and loading it from there, but thats too clunky and slow

Hi,

I recommend an ObjC solution.

¢ In Xcode, create new class files with Menu File > New File > Objective-C class with name ImageLoader
¢ Replace the contents of ImageLoader.h with

[code]#import <Cocoa/Cocoa.h>

@interface ImageLoader : NSObject {

}

  • (void)loadImageInImageView:(NSImageView *)theImageView fromURL:(NSString *)url;
    @end[/code]
    ¢ Replace the contents of ImageLoader.m with

[code]#import “ImageLoader.h”

@implementation ImageLoader

  • (void)loadImageInImageView:(NSImageView *)theImageView fromURL:(NSString *)url
    {
    NSImage *newImage;
    NSURL *imageURL = [NSURL URLWithString:url];
    NSData *imageData = [NSData dataWithContentsOfURL];
    if (imageData != nil) {
    newImage = [[NSImage alloc] initWithData:imageData];
    [theImageView setImage];
    [newImage release];
    }
    }

@end[/code]
¢ Use this line in your AppleScript code


    call method "loadImageInImageView:fromURL:" of class "ImageLoader" with parameters {imageView, imageURL}

imageView is a reference to the Image View
imageURL is the string URL of the image

Thanks so much, that worked perfectly!! :slight_smile:

Hi There,

Whats wrong in my Code:


set imageURL to "http://adele.gerwinski.de/~anja/gnuart/a-gnu-head/a-gnu-head.png"

call method "loadImageInImageView:fromURL:" of class "ImageLoader" with parameters {imageView, imageURL}

Can you help me?

have you defined the property imageView and assigned the reference to the Image View