0

I use AFNetworking to get beer details from my rails api, and I want to show an image from a url that is returned.

Example API response

    description = "Super tasty";
    id = 3;
    "label_url" = "/system/beers/labels/000/000/003/medium/Pliny.png?1363034728";
    "location_id" = 2;
    name = "Pliny The Elder";
    price = "3.5";
    style = IPA;

The label_url is the url for the image, which I'm trying to display in the BeerDetailViewController. Here is what I've tried so far, which builds with no errors but no image displays.

BeerDetailViewController.m

#import "UIImageView+AFNetworking.h"
#import "AFImageRequestOperation.h"

- (void)viewDidLoad
{
    [super viewDidLoad];

    beerLabel.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:
                                              [NSURL URLWithString: beer_label]]];

Any ideas why it's not working? I can display the url in this view with something like beerLabel.text = beer_label so I know it's coming through and read to be used, just not sure why it isn't displaying an image.

jacobt
  • 157
  • 3
  • 11

2 Answers2

5
/system/beers/labels/000/000/003/medium/Pliny.png?1363034728

is not a valid URL. Seriously, how would poor NSURL and NSData know what host are you looking for? You need to construct a proper URL string using the host and this path.

0

What you have there is only the path portion of a URL. You want to resolve this against the correct base URL. So let's say your server is example.com. To resolve it:

NSURL *base = [NSURL URLWithString:@"http://example.com"];
NSURL *resolved = [NSURL URLWithString:beer_label relativeToURL:base];
Mike Abdullah
  • 14,933
  • 2
  • 50
  • 75