Objective-C how to get an image from a url
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 u开发者_StackOverflow社区sing, 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?
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:
.
精彩评论