4

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

nosedive25
  • 2,477
  • 5
  • 30
  • 45

1 Answers1

5

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
  • 2,465
  • 6
  • 25
  • 34
  • Hmmm, I get an error when I try NSImage *myImage = [NSImage initWithContentsOfURL:[NSURL URLWithString:@"http://somesite.com/image.png"]]; Any ideas? – nosedive25 Apr 18 '10 at 02:42
  • I get +[NSImage initWithContentsOfURL:]: unrecognized selector sent to class 0x7fff704e6340 – nosedive25 Apr 18 '10 at 02:50
  • Are you doing this for an iphone? Did you mean to be using UIImage instead of NSImage? – Michael Shnitzer Apr 18 '10 at 03:01
  • 2
    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 Apr 18 '10 at 03:27