1

I'm having problems pulling in images from a Yummly API using AFNetworking. Is the problem because some values don't have a url? Anyone with ideas?

The last line in the method is the one in question.

- (void)configureForSearchResult:(SearchResult *)searchResult
{
    self.recipeNameLabel.text = searchResult.recipeName;
    self.ratingLabel.text = searchResult.rating;
    [self.snapImageView setImageWithURL:[NSURL URLWithString:searchResult.image] placeholderImage: [UIImage imageNamed:@"Placeholder"]];
}

This is how I'm parsing:

- (SearchResult *)parseRecipe:(NSDictionary *)dictionary
{
    SearchResult *searchResult = [[SearchResult alloc] init];

    searchResult.recipeName = [dictionary objectForKey:@"recipeName"];
    searchResult.rating = [NSString stringWithFormat:@"%@", [dictionary objectForKey:@"rating"]];
    searchResult.image = [NSString stringWithFormat:@"%@",[dictionary objectForKey:@"smallImageUrls"]];

    NSLog(@"%@", searchResult.image);

    return searchResult;
}

This is what I'm getting from the API as a result of the NSLog above:

2013-06-09 22:06:19.268 Yummly[90606:11303] ("http://i.yummly.com/Game-day-bbq-chicken-mini-pizzas-310087-274540.s.jpg" )

2013-06-09 22:06:19.268 Yummly[90606:11303] ( )

2013-06-09 22:06:19.271 Yummly[90606:11303] ( "http://i.yummly.com/Buffalo-Chicken-Pizza-Food-Network-2.s.jpg" )

2013-06-09 22:06:19.273 Yummly[90606:11303] ( )

2013-06-09 22:06:19.275 Yummly[90606:11303] ( "http://i.yummly.com/Julia-child_s-eggplant-pizzas-_tranches-d_aubergine-a-l_italienne_-309207-273660.s.jpg" )

Here's an example of the returned JSON:

{
  "attribution": {
    "html": "<a href='http:\/\/www.yummly.com\/recipes\/pizza'>pizza recipes<\/a> search powered by <img alt='Yummly' src='http:\/\/static.yummly.com\/api-logo.png'\/>",
    "url": "http:\/\/www.yummly.com\/recipes\/pizza",
    "text": "pizza recipes: search powered by Yummly",
    "logo": "http:\/\/static.yummly.com\/api-logo.png"
  },
  "totalMatchCount": 8591,
  "facetCounts": {

  },
  "matches": [
    {
      "attributes": {
        "course": [
          "Main Dishes",
          "Appetizers",
          "Lunch and Snacks"
        ],
        "cuisine": [
          "Mediterranean",
          "Greek"
        ]
      },
      "flavors": null,
      "rating": 5,
      "id": "Greek-pizza-333514",
      "smallImageUrls": [
        "http:\/\/i.yummly.com\/Greek-pizza-333514-295434.s.jpg"
      ],
      "sourceDisplayName": "How Sweet It Is",
      "totalTimeInSeconds": null,
      "ingredients": [
        "red onion",
        "olive oil",
        "pizza doughs",
        "artichoke hearts",
        "roasted red pepper",
        "kalamata olives",
        "feta",
        "garlic cloves",
        "fresh dill",
        "tomatoes",
        "mozzarella cheese"
      ],
      "recipeName": "Greek Pizza"
    },
    {
      "attributes": {

      },
      "flavors": {
        "salty": 0.66666666666667,
        "sour": 0.5,
        "sweet": 0.16666666666667,
        "bitter": 0.5,
        "meaty": 0.83333333333333,
        "piquant": 0
      },
      "rating": 5,
      "id": "Breakfast-pizza-305693",
      "smallImageUrls": [
        "http:\/\/i.yummly.com\/Breakfast-pizza-305693-270301.s.jpg"
      ],
      "sourceDisplayName": "Smitten Kitchen",
      "totalTimeInSeconds": null,
      "ingredients": [
        "shallot",
        "large eggs",
        "bacon",
        "ground black pepper",
        "parmesan",
        "flat-leaf parsley",
        "yeast",
        "kosher salt",
        "scallions",
        "bread flour",
        "mozzarella",
        "chives"
      ],
      "recipeName": "Breakfast Pizza"
    },
    {
      "attributes": {

      },
      "flavors": null,
      "rating": 4,
      "id": "Mini-Deep-Dish-Pizzas-Martha-Stewart-191946",
      "smallImageUrls": [
        "http:\/\/i.yummly.com\/Mini-Deep-Dish-Pizzas-Martha-Stewart-191946-104372.s.png"
      ],
      "sourceDisplayName": "Martha Stewart",
      "totalTimeInSeconds": 1800,
      "ingredients": [
        "coarse salt",
        "olive oil",
        "ground pepper",
        "vegetables",
        "tomato",
        "all-purpose flour",
        "pizza doughs",
        "shredded mozzarella"
      ],
Will
  • 207
  • 1
  • 5
  • 11

6 Answers6

0

URL in cocoa are used as alternative to paths, so you can only use file URLs. If you want to display data from a remote source you have to load it from the source using NSURLConnection, NSURLRequest and then either convert the data to a UIImage or save it to disk and the use a file URL for the local copy of the image.

Nathan Day
  • 5,981
  • 2
  • 24
  • 40
  • Thanks, I'll give these a shot. I really wanted to try the AFNetworking methods out of the box. – Will Jun 10 '13 at 05:57
0

There is actually nothing wrong in your code.The problem is that the webservice is not retuning all the images . setImageWithURL: placeholderImage: is a fine peice of code that will load the images asynchronously with AFNetworking

Lithu T.V
  • 19,955
  • 12
  • 56
  • 101
0

Your JSON response for key "smallImageUrls"

"smallImageUrls": ["http:\/\/i.yummly.com\/Breakfast-pizza-305693-270301.s.jpg"]

it's not a string,so use

searchResult.image = [NSString stringWithFormat:@"%@",[[dictionary objectForKey:@"smallImageUrls"] objectAtIndex:0]];

// [] - it represents array in response
NANNAV
  • 4,875
  • 4
  • 32
  • 50
0

Probably some images are blank so you might opt to use placeholder image. Also see the encoding of the image whether it is base64 encoding. If it is then use this code and replace it with yours.

NSString *base64StringEncoder = @"data:image/png;base64,";
    base64StringEncoder = [base64StringEncoder stringByAppendingString:[[presentationArray objectAtIndex:indexPath.row]valueForKey:@"profile_image"]];

    NSURL *profilePicURL = [NSURL URLWithString:base64StringEncoder];
    NSData *profilePicimageData = [NSData dataWithContentsOfURL:profilePicURL];
    if (profilePicimageData.length!=0) {
        cell.imgView.image = [UIImage imageWithData:profilePicimageData];
    }
0

The problem is that Objective-C doesn't percent encode your string automatically. Your URL contains characters that should be encoded. Have a look at NSURL Encoding in ObjC.

Community
  • 1
  • 1
Dunes Buggy
  • 1,779
  • 1
  • 21
  • 41
0

The problem is with your parsing code, you try to parse an array in to a string.

- (SearchResult *)parseRecipe:(NSDictionary *)dictionary
{
    SearchResult *searchResult = [[SearchResult alloc] init];

    searchResult.recipeName = [dictionary objectForKey:@"recipeName"];
    // You should just store the  rating as a NSNumber since that is what it is.
    searchResult.rating = [NSString stringWithFormat:@"%@", [dictionary objectForKey:@"rating"]];

    NSArray *imageArray = [dictionary objectForKey:@"smallImageUrls"];
    if ([imageArray count] > 0) {
       searchResult.image = [imageArray objectAtIndex:0];
    }

    return searchResult;
}

Then just before your try and set the image check if you have an image:

- (void)configureForSearchResult:(SearchResult *)searchResult
{
    if (!searchResult.image) {
        return;
    }

    self.recipeNameLabel.text = searchResult.recipeName;
    self.ratingLabel.text = searchResult.rating;
    [self.snapImageView setImageWithURL:[NSURL URLWithString:searchResult.image] placeholderImage: [UIImage imageNamed:@"Placeholder"]];
}
rckoenes
  • 69,092
  • 8
  • 134
  • 166