1

AFNetworking を使用して Yummly API から画像を取り込むのに問題があります。一部の値に URL がないことが問題ですか? アイデアのある人はいますか?

メソッドの最後の行が問題の行です。

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

問題は解析コードにあり、配列を文字列に解析しようとします。

- (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 に答える
0

cocoa の URL はパスの代わりに使用されるため、ファイル URL のみを使用できます。リモート ソースからデータを表示する場合は、NSURLConnection、NSURLRequest を使用してソースからデータをロードし、データを UIImage に変換するか、ディスクに保存して、画像のローカル コピーのファイル URL を使用する必要があります。

于 2013-06-10T05:30:26.093 に答える
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

コードには実際には何も問題はありません。問題は、Web サービスがすべての画像を再調整していないことです。setImageWithURL: placeholderImage:画像を非同期的にロードする素晴らしいコードですAFNetworking

于 2013-06-10T06:00:53.183 に答える