AFNetworking - setImageWithURL不显示图像

时间:2013-06-10 05:18:47

标签: ios objective-c afnetworking

我在使用AFNetworking从Yummly API中提取图像时遇到问题。问题是因为有些值没有网址吗?有想法的人吗?

方法中的最后一行是有问题的一行。

- (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"]];
}

这就是我解析的方式:

- (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;
}

这是我从上面的NSLog得到的API:

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” )

以下是返回的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"
      ],

6 个答案:

答案 0 :(得分:0)

cocoa中的URL用作路径的替代,因此您只能使用文件URL。如果要显示远程源中的数据,则必须使用NSURLConnection,NSURLRequest从源加载数据,然后将数据转换为UIImage或将其保存到磁盘,并使用文件URL作为映像的本地副本。

答案 1 :(得分:0)

您的代码实际上没有任何问题。问题是网络服务没有重新调整所有图像。 setImageWithURL: placeholderImage:是一个很好的代码片段,可以与AFNetworking异步加载图像

答案 2 :(得分:0)

您对密钥“smallImageUrls”的

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

它不是字符串,所以请使用

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

// [] - it represents array in response

答案 3 :(得分:0)

可能有些图片是空白的,因此您可以选择使用占位符图片。 还可以看到图像的编码是否是base64编码。 如果是,那么使用此代码并将其替换为您的代码。

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];
    }

答案 4 :(得分:0)

问题是Objective-C不会自动对字符串进行百分比编码。您的URL包含应编码的字符。看看NSURL Encoding in ObjC

答案 5 :(得分:0)

问题在于您的解析代码,您尝试将数组解析为字符串。

- (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;
}

然后在尝试之前设置图像检查是否有图像:

- (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"]];
}