views:

70

answers:

1

Hello,

I need to create an NSImage from a url and then set it to an image view in my application. I tried some code I found online but it didn't work. If anyone knows how to do this any help would be great.

Thanks

A: 

You'll want to look at

 initWithContentsOfURL:
Initializes and returns an NSImage instance with the contents of the specified URL.

- (id)initWithContentsOfURL:(NSURL *)aURL

Parameters
aUrl
The URL identifying the image.
Return Value
An initialized NSImage instance, or nil if the method cannot create an image representation from the contents of the specified URL.

From http://developer.apple.com/mac/library/documentation/cocoa/Reference/ApplicationKit/Classes/NSImage_Class/Reference/Reference.html#//apple_ref/occ/instm/NSImage/initWithContentsOfURL:

Michael Shnitzer
Hmmm, I get an error when I try NSImage *myImage = [NSImage initWithContentsOfURL:[NSURL URLWithString:@"http://somesite.com/image.png"]]; Any ideas?
happyCoding25
What type of error?
Michael Shnitzer
I get +[NSImage initWithContentsOfURL:]: unrecognized selector sent to class 0x7fff704e6340
happyCoding25
Are you doing this for an iphone? Did you mean to be using UIImage instead of NSImage?
Michael Shnitzer
No I got it to load thanks
happyCoding25
happyCoding: `initWithContentsOfURL:` is an instance method. You need to send that message to an instance—specifically, one just received from `alloc`. The NSImage *class* does not respond to it (i.e., it isn't a class method), as evidenced by that exception message.
Peter Hosey