tags:

views:

25

answers:

1

Hi,

Am having some difficulty getting an Image from a url, and then displaying it in an image well in the interface.

This is the code I'm currently using, it doesn't work and compiles with no errors:

NSURL *url = [NSURL URLWithString:@"http://www.nataliedee.com/061105/hey-whale.jpg"];

NSString *newIMAGE = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];

[imageView setImage:[NSImage imageNamed:newIMAGE]];

Any ideas as to what is wrong here?

+3  A: 

You are passing in image data as a string to a method that is trying to use that string as the name of the image, which of course doesn't exist.

What you need to do is create an NSData object from your URL dataWithContentsOfURL:, once you have the NSData object use this to create the UIImage via imageWithData:.

MarkPowell
+1, except you meant `imageWithData:` (fixed for you)
Dave DeLong
but I'm not doing iPhone dev, should it just be NSImage via imageNamed for mac dev?
James Eggers
@James Use `NSImage * image = [[NSImage alloc] initWithData:theDownloadedData];`
Dave DeLong
thanks, it worked :)
James Eggers