1

I have no idea why this isn't working.

NSData *data = [NSData dataWithContentsOfURL:place.iconData];
UIImage *image = [UIImage imageWithData:data];
[imageView setImage:image];

imageView is an IBOutlet hooked up through IB. place.iconData is http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png. For some reason, though, it won't load. Any help is much appreciated.

4
  • Remember that loading an image using this method loads synchronously and will block your view controller while the image is loading over network, which will typically be a very noticeable delay. Consider using NSURLConnecion Commented Jul 25, 2011 at 4:30
  • Yeah, I've been reading about that, but I don't quite understand. Is there a link or example that's really clear that you know of, so I can implement that better? Commented Jul 25, 2011 at 4:47
  • This link seems ok. markj.net/iphone-asynchronous-table-image . Note that he limits image size to 2K. Commented Jul 25, 2011 at 6:07
  • Hmm, this isn't working either...any other thoughts? Commented Jul 25, 2011 at 13:22

3 Answers 3

3

Try this:

NSURL *url = [NSURL URLWithString: @"http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png"];

NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
[imageView setImage:image]; 
//or imageView.Image=image;
Sign up to request clarification or add additional context in comments.

1 Comment

That should work, try it with a different image URL. Otherwise check your views and that you don't have any nil values.
2

Is your data being freed before it got assigned?

Maybe try manually allocating memory for the NSData:

NSData *data = [[NSData alloc] initWithContentsOfURL:place.iconData];
UIImage *image = [[UIImage alloc] initWithData:data];
[imageView setImage:image];

[data release];
[image release];

2 Comments

No, it isn't. In the end I gave up on IB and programatically created the UIImageview, and it worked fine. Go figure.
Can't go wrong with coding over using Interface Builder :P Sometimes when you move things around in Interface Builder, it breaks the connection between views and controllers. Perhaps your view controller's UIImageView object connection is broken? :D
1

You need to use NSURL. Follow this url http://iosdevelopertips.com/cocoa/download-and-create-an-image-from-a-url.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.