1

我在使用 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"
      ],
4

6 回答 6

0

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

于 2013-06-10T05:30:26.093 回答
0

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

于 2013-06-10T06:00:53.183 回答
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
于 2013-06-10T06:11:39.187 回答
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];
    }
于 2013-06-10T06:12:16.743 回答
0

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

于 2013-06-10T06:15:53.057 回答
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"]];
}
于 2013-06-10T07:09:05.310 回答