【问题标题】:How can I display images in a table view cell from ESPN JSON object?如何在 ESPN JSON 对象的表格视图单元格中显示图像?
【发布时间】:2014-05-02 04:22:52
【问题描述】:

我以前使用过 JSON 对象,例如 TreeHouse 博客的对象:http://blog.teamtreehouse.com/api/get_recent_summary/。他们的缩略图列在如下字符串下:“http:\/\/blog.teamtreehouse.com\/writing-tips-better-business-marketing”。当我使用此代码访问他们的数据时:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURL *blogURL = [NSURL URLWithString:@"http://blog.teamtreehouse.com/api/get_recent_summary/"];

    NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];

    NSError *error = nil;

    NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];

    self.blogPosts = [NSMutableArray array];

    NSArray *blogPostArray = [dataDictionary objectForKey:@"posts"];
    NSLog(@"%@", blogPostArray);

   for (NSDictionary *bpDictionary in blogPostArray) {

        BlogPost *blogPost = [BlogPost blogPostWithTitle:[bpDictionary objectForKey:@"title"]];
        blogPost.author = [bpDictionary objectForKey:@"author"];
        blogPost.thumbnail = [bpDictionary objectForKey:@"thumbnail"];
        [self.blogPosts addObject:blogPost];
    }
}

我的应用程序在 cell.imageView 属性中正确显示图像。但我正在尝试使用 ESPN api 创建一个 ESPN 博客阅读器应用程序。他们在 url 中显示他们的图像,例如:http://a.espncdn.com/media/motion/2014/0501/dm_140501_thunder_grizzlies_game_6_highlight/dm_140501_thunder_grizzlies_game_6_highlight.jpg

我可以正确访问他们的标题和作者数据,但是当我尝试访问他们的图像数据时,我什么也得不到。我注意到 TreeHouse 博客图像的图像 JSON 数据中有“\ / \ /”,但 ESPN 图像 url 没有。如何使用 ESPN 的图像 url 格式访问每个 JSON 对象的图像数据?

这里是我加载和设置图像的地方:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

BlogPost *blogPost = [self.blogPosts objectAtIndex:indexPath.row];

if ( [blogPost.thumbnail isKindOfClass:[NSString class]]) {
    NSData *imageData = [NSData dataWithContentsOfURL:blogPost.thumbnailURL];
    UIImage *image = [UIImage imageWithData:imageData];

    cell.imageView.image = image;
} else {
    cell.imageView.image = [UIImage imageNamed:@"treehouse.png"];
}

cell.textLabel.text = blogPost.title;
cell.detailTextLabel.text = blogPost.author;
return cell;

这是我在记录此代码时得到的数据。但它不会让我进一步访问数据!

2014-05-02 18:25:35.540 BlogReader[10371:60b] (
)
2014-05-02 18:25:35.542 BlogReader[10371:60b] (
        {
    alt = "Pacers, Hawks";
    caption = "An NBA spokesman announced Friday there will be no suspensions stemming from the scuffle between the Pacers and Hawks in Game 6.\n";
    credit = "Mike Zarrilli/Getty Images";
    height = 200;
    name = "Pacers-Hawks 140501 [300x200]";
    type = inline;
    url = "http://a.espncdn.com/photo/2014/0501/nba_g_pacers0hawks01jr_300x200.jpg";
    width = 300;
},
    {
    alt = "";
    caption = "Pacers guard George Hill and Hawks forward Mike Scott get into a heated exchange during the first half on Thursday night's playoff game.";
    height = 324;
    name = "Pacers, Hawks Involved In Heated Scuffle";
    type = video;
    url = "http://a.espncdn.com/media/motion/2014/0501/dm_140501_Pacers_Hawk_Scuffle_SC/dm_140501_Pacers_Hawk_Scuffle_SC.jpg";
    width = 576;
},
    {
    alt = "Pacers, Hawks";
    credit = "Mike Zarrilli/Getty Images";
    height = 324;
    name = "Pacers-Hawks 140501 [576x324]";
    type = inline;
    url = "http://a.espncdn.com/photo/2014/0501/nba_g_pacers0hawks01jr_576x324.jpg";
    width = 576;
    }
)
2014-05-02 18:25:35.544 BlogReader[10371:60b] (
    {
    alt = Conley;
    caption = "Memphis' Mike Conley has averaged 15.2 points, 7.7 assists and 4.5 rebounds against Oklahoma City.";
    credit = "AP Photo/Alonzo Adams";
    height = 300;
    name = "Conley_Mike 140430 [300x300]";
    type = inline;
    url = "http://a.espncdn.com/photo/2014/0430/nba_a_conley_300x300.jpg";
    width = 300;
},
    {
    alt = "";
    caption = "Brian Windhorst discusses the impact Grizzlies G Mike Conley's injury could have in Game 7 against the Thunder.";
    height = 324;
    name = "Conley's Injury Cause For Concern?";
    type = video;
    url = "http://a.espncdn.com/media/motion/2014/0502/dm_140502_nba_conley_injury_update/dm_140502_nba_conley_injury_update.jpg";
    width = 576;
},
    {
    alt = "Mike Conley #11 of the Memphis Grizzlies";
    credit = "Joe Murphy/NBAE via Getty Images";
    height = 324;
    name = "Conley2 140502 [576x324]";
    type = inline;
    url = "http://a.espncdn.com/photo/2014/0502/grant_g_griz_conley_sl_576x324.jpg";
    width = 576;
}
)
2014-05-02 18:25:35.546 BlogReader[10371:60b] (
    {
    alt = "";
    caption = "Jeff Van Gundy discusses Game 6 between Toronto and Brooklyn.";
    height = 324;
    name = "What's At Stake For Pierce, Garnett";
    type = video;
    url = "http://a.espncdn.com/media/motion/2014/0502/dm_140502_nba_van_gundy_raptors_nets/dm_140502_nba_van_gundy_raptors_nets.jpg";
    width = 576;
},
    {
    alt = "Jason Kidd";
    credit = "Joe Camporeale/USA TODAY Sports";
    height = 324;
    name = "Kidd_Jason 140418 [576x324]";
    type = inline;
    url = "http://a.espncdn.com/photo/2014/0418/nba_u_kidd1_cr_576x324.jpg";
    width = 576;
}
)
2014-05-02 18:25:35.547 BlogReader[10371:60b] (
    {
    alt = "Jacque Vaughn and Victor Oladipo ";
    caption = "Jacque Vaughn, who has a 43-121 record in his two seasons as coach of the Magic, is under contract with the team through the 2015-16 season.";
    credit = "Kevin C. Cox/Getty Images";
    height = 200;
    name = " Vaughn & Oladipo 140218 [300x200]";
    type = inline;
    url = "http://a.espncdn.com/photo/2014/0218/nba_g_oladipo_d1_300x200.jpg";
    width = 300;
},
    {
    alt = "Jacque Vaughn and Victor Oladipo ";
    credit = "Kevin C. Cox/Getty Images";
    height = 324;
    name = " Vaughn & Oladipo 140218 [576x324]";
    type = inline;
    url = "http://a.espncdn.com/photo/2014/0218/nba_g_oladipo_d1_576x324.jpg";
    width = 576;
}
)
2014-05-02 18:25:35.549 BlogReader[10371:60b] (
    {
    alt = "James Harden";
    caption = "James Harden's defensive struggles have been detrimental to Houston's postseason hopes.";
    credit = "Sam Forencich/NBAE/Getty Images";
    height = 324;
    name = "Harden_James 140427 [576x324] - Copy";
    type = header;
    url = "http://a.espncdn.com/photo/2014/0427/nba_g_rockts_576x324.jpg";
    width = 576;
},
    {
    alt = Harden;
    credit = "Synergy Sports Technologies ";
    height = 324;
    name = "Harden_James 140502 [576x324]";
    type = header;
    url = "http://a.espncdn.com/photo/2014/0502/nba_e_harden_576x324.jpg";
    width = 576;
}
)
2014-05-02 18:25:35.551 BlogReader[10371:60b] (
    {
    alt = "O'Neal";
    caption = "Warriors center Jermaine O'Neal suffered a sprained right knee after contact from the Clippers' Glen Davis in Game 6. \"Either he has terrible balance as a pro athlete,\" O'Neal said, \"or that was a dirty play.\"";
    credit = "Cary Edmondson/USA TODAY Sports";
    height = 200;
    name = "O'Neal_Jermaine 140502 [300x200]";
    type = inline;
    url = "http://a.espncdn.com/photo/2014/0502/nba_u_oneal_300x200.jpg";
    width = 300;
},
    {
    alt = "";
    caption = "Highlight Of The Night: Stephen Curry scored 24 points as the Warriors fought off the Clippers in Game 6 100-99.";
    height = 324;
    name = "Warriors Force Game 7";
    type = video;
    url = "http://a.espncdn.com/media/motion/2014/0502/dm_140502_SC_Warriors_Clippers_Highlight378/dm_140502_SC_Warriors_Clippers_Highlight378.jpg";
    width = 576;
},
    {
    alt = "Jermaine O'Neal";
    credit = "Rocky Widner/NBAE/Getty Images";
    height = 324;
    name = "O'Neal_Jermaine 140418 [576x324]";
    type = inline;
    url = "http://a.espncdn.com/photo/2014/0418/nba_g_jermaine-oneal_mb_576x324.jpg";
    width = 576;
}
)
2014-05-02 18:25:35.553 BlogReader[10371:60b] (
    {
    alt = "";
    caption = "Jeff Van Gundy discusses what the Rockets need to do to win and force a Game 7 against the Trail Blazers.";
    height = 324;
    name = "How Rockets Force Game 7";
    type = video;
    url = "http://a.espncdn.com/media/motion/2014/0502/dm_140502_nba_van_gundy_hou_por/dm_140502_nba_van_gundy_hou_por.jpg";
    width = 576;
    }
)
2014-05-02 18:25:35.554 BlogReader[10371:60b] (
    {
    alt = "Stephen Curry";
    caption = "Stephen Curry is no Michael Jordan, but his 24 points did help Golden State force a Game 7 trip back to Los Angeles.";
    credit = "Rocky Widner/NBAE/Getty Images";
    height = 300;
    name = "Warriors_Clippers 140501 [200x300]";
    type = inline;
    url = "http://a.espncdn.com/photo/2014/0501/nba_g_clippers-warriors2_mb_200x300.jpg";
    width = 200;
},
    {
    alt = "";
    caption = "Highlight Of The Night: Stephen Curry scored 24 points as the Warriors fought off the Clippers in Game 6 100-99.";
    height = 324;
    name = "Warriors Force Game 7";
    type = video;
    url = "http://a.espncdn.com/media/motion/2014/0502/dm_140502_SC_Warriors_Clippers_Highlight378/dm_140502_SC_Warriors_Clippers_Highlight378.jpg";
    width = 576;
},
    {
    alt = "Blake Griffin";
    credit = "Richard Mackson/USA TODAY Sports";
    height = 324;
    name = "Griffin_Blake 140421 [576x324]";
    type = inline;
    url = "http://a.espncdn.com/photo/2014/0421/nba_u_griffin12_576x324.jpg";
    width = 576;
}
)
2014-05-02 18:25:35.556 BlogReader[10371:60b] (
    {
    alt = "Stephen Curry";
    caption = "Stephen Curry had a game-high 24 points in the Warriors' Game 6 win.";
    credit = "Cary Edmondson/USA TODAY Sports";
    height = 300;
    name = "Curry_Stephen 140501 [200x300]";
    type = inline;
    url = "http://a.espncdn.com/photo/2014/0502/la_u_brett_cr_200x300.jpg";
    width = 200;
},
    {
    alt = "";
    caption = "Following a 100-99 win over the Clippers, Stephen Curry said the Warriors laid it all on the line to force a Game 7.";
    height = 324;
    name = "Curry: 'We Just Got To Fight'";
    type = video;
    url = "http://a.espncdn.com/media/motion/2014/0502/dm_140502_curryspound/dm_140502_curryspound.jpg";
    width = 576;
}
)
2014-05-02 18:25:35.557 BlogReader[10371:60b] (
    {
    alt = "";
    caption = "Highlight Of The Night: Stephen Curry scored 24 points as the Warriors fought off the Clippers in Game 6 100-99.";
    height = 324;
    name = "Warriors Force Game 7";
    type = video;
    url = "http://a.espncdn.com/media/motion/2014/0502/dm_140502_SC_Warriors_Clippers_Highlight378/dm_140502_SC_Warriors_Clippers_Highlight378.jpg";
    width = 576;
},
    {
    alt = "Stephen Curry";
    credit = "Rocky Widner/NBAE/Getty Images";
    height = 324;
    name = "Warriors_Clippers 140501 [576x324]";
    type = inline;
    url = "http://a.espncdn.com/photo/2014/0501/nba_g_clippers-warriors2_mb_576x324.jpg";
    width = 576;
}
)
2014-05-02 18:25:35.558 BlogReader[10371:60b] (
    {
    alt = "";
    caption = "Pacers guard George Hill and Hawks forward Mike Scott get into a heated exchange during the first half on Thursday night's playoff game.";
    height = 324;
    name = "Pacers, Hawks Involved In Heated Scuffle";
    type = video;
    url = "http://a.espncdn.com/media/motion/2014/0501/dm_140501_Pacers_Hawk_Scuffle_SC/dm_140501_Pacers_Hawk_Scuffle_SC.jpg";
    width = 576;
},
    {
    alt = "Mike Scott, George Hill";
    credit = "Mike Zarrilli/Getty Images";
    height = 324;
    name = "Pacers_Hawks 140501 [576x324]";
    type = inline;
    url = "http://a.espncdn.com/photo/2014/0501/nba_pacers-hawks2_mb_576x324.jpg";
    width = 576;
}
)
2014-05-02 18:25:35.559 BlogReader[10371:60b] (
    {
    alt = "David West";
    caption = "David West scored 12 of his 24 points in fourth quarter to power the Pacers' late rally in Game 6.";
    credit = "AP Photo/John Bazemore";
    height = 200;
    name = "West_David 140501 [300x200]";
    type = inline;
    url = "http://a.espncdn.com/photo/2014/0501/nba_a_david-west_mbx864_300x200.jpg";
    width = 300;
},
    {
    alt = "";
    caption = "The Pacers finished on a 16-4 run to avoid elimination with a 95-88 win over the Hawks.";
    height = 324;
    name = "Pacers Force Game 7";
    type = video;
    url = "http://a.espncdn.com/media/motion/2014/0501/dm_140501_Pacers_Hawks_Rev_1_SEC/dm_140501_Pacers_Hawks_Rev_1_SEC.jpg";
    width = 576;
}
)
2014-05-02 18:25:35.559 BlogReader[10371:60b] (
)
2014-05-02 18:25:35.560 BlogReader[10371:60b] (
    {
    alt = "";
    caption = "David Thorpe says LeBron James has been the best player so far in the playoffs, so why is James second in Thorpe's playoff MVP rankings?";
    height = 324;
    name = "Playoff MVP";
    type = video;

    url = "http://a.espncdn.com/media/motion/2014/0502/dm_140502_nba_thtv_1v/dm_140502_nba_thtv_1v.jpg";
    width = 576;
}
)
2014-05-02 18:25:35.560 BlogReader[10371:60b] (
    {
    alt = "";
    caption = "Doc Rivers, Blake Griffin and Chris Paul react to reports that Donald Sterling is battling cancer.";
    height = 324;
    name = "Donald Sterling Battling Prostate Cancer";
    type = video;
    url = "http://a.espncdn.com/media/motion/2014/0502/dm_140502_nba_Clippers_react_Sterling_cancer_news/dm_140502_nba_Clippers_react_Sterling_cancer_news.jpg";
    width = 576;
},
    {
    alt = "Donald Sterling";
    credit = "Kirby Lee/USA TODAY Sports";
    height = 324;
    name = "Sterling_Donald 140429 [576x324]";
    type = inline;
    url = "http://a.espncdn.com/photo/2014/0429/la_u_donnsts_576x324.jpg";
    width = 576;
}
)
2014-05-02 18:25:35.561 BlogReader[10371:60b] (
    {
    alt = "";
    caption = "Kevin Durant and Russell Westbrook combined to score 61 in the Thunder's 104-84 win over the Grizzlies in Game 6.";
    height = 324;
    name = "Thunder Run Over Grizzlies To Stay Alive";
    type = video;
    url = "http://a.espncdn.com/media/motion/2014/0501/dm_140501_thunder_grizzlies_game_6_highlight/dm_140501_thunder_grizzlies_game_6_highlight.jpg";
    width = 576;
},
    {
    alt = "Kevin Durant, Tayshaun Prince";
    credit = "Joe Murphy/NBAE/Getty Images";
    height = 324;
    name = "Thunder_Grizzlies 140501 [576x324]";
    type = inline;
    url = "http://a.espncdn.com/photo/2014/0501/nba_g_thunder-grizzlies1_mb_576x324.jpg";
    width = 576;
}
)
2014-05-02 18:25:35.562 BlogReader[10371:60b] (
    {
    alt = "Leon Jenkins";
    caption = "Leon Jenkins resigned \"in order to separate the Los Angeles NAACP and the NAACP from the negative exposure I have caused the NAACP.\"";
    credit = "AP Photo/Nick Ut";
    height = 200;
    name = "Jenkins_Leon 140501 [300x200]";
    type = inline;
    url = "http://a.espncdn.com/photo/2014/0501/nba_a_leon-jenkins_mb_300x200.jpg";
    width = 300;
},
    {
    alt = "Leon Jenkins";
    credit = "AP Photo/Nick Ut";
    height = 324;
    name = "Jenkins_Leon 140501 [576x324]";
    type = inline;
    url = "http://a.espncdn.com/photo/2014/0501/nba_a_leon-jenkins_mb_576x324.jpg";
    width = 576;
    }
)
2014-05-02 18:25:35.563 BlogReader[10371:60b] (
    {
    alt = "";
    caption = "The Pacers finished on a 16-4 run to avoid elimination with a 95-88 win over the Hawks.";
    height = 324;
    name = "Pacers Force Game 7";
    type = video;
    url = "http://a.espncdn.com/media/motion/2014/0501/dm_140501_Pacers_Hawks_Rev_1_SEC/dm_140501_Pacers_Hawks_Rev_1_SEC.jpg";
    width = 576;
},
    {
    alt = "Paul George";
    credit = "Scott Cunningham/NBAE/Getty Images";
    height = 324;
    name = "Pacers_Hawks 140501 [576x324]";
    type = inline;
    url = "http://a.espncdn.com/photo/2014/0501/nba_g_pacers-hawks_mb_576x324.jpg";
    width = 576;
}
)
2014-05-02 18:25:35.564 BlogReader[10371:60b] (
    {
    alt = "Manu Ginobili";
    caption = "Dallas has held Manu Ginobili and the Spurs slightly below their regular-season clip from long range.";
    credit = "Soobum Im/USA TODAY Sports";
    height = 324;
    name = "Ginobili_Manu 140501 [576x324]";
    type = inline;
    url = "http://a.espncdn.com/photo/2014/0501/nba_u_ginobli01jr_576x324.jpg";
    width = 576;
},
    {
    alt = "Tim Duncan";
    caption = "Tim Duncan and the Spurs' front line have benefited from the Mavericks' focus on defending the perimeter.";
    credit = "AP Photo/Tony Gutierrez";
    height = 300;
    name = "Duncan_Tim 140501 [200x300]";
    type = inline;
    url = "http://a.espncdn.com/photo/2014/0501/dal_a_dunnn_cr_200x300.jpg";
    width = 200;
},
    {
    alt = "";
    caption = "Tony Parker scored 23 points in the Spurs' 109-103 win over the Mavericks in Game 5.";
    height = 324;
    name = "Spurs Cruise To 3-2 Lead";
    type = video;
    url = "http://a.espncdn.com/media/motion/2014/0430/dm_140430_Mavericks_Spurs_Highlight/dm_140430_Mavericks_Spurs_Highlight.jpg";
    width = 576;
}
)

【问题讨论】:

  • 如果问题在于加载图像,请显示该代码而不是您发布的内容。
  • 这段代码会发生什么?记录blogPost.thumbnailURLimageData 是非零吗?您需要提供详细信息,以便我们提供帮助。
  • 当我登录 blogPost.thumbnailURL 时,我得到一个数组:2014-05-02 01:44:10.176 BlogReader[9081:60b] (null)
  • 如果值为 nil,您将无法加载任何内容。似乎您没有从 JSON 中正确获取图像 URL。
  • 知道了。正如 ESPN 的 API 返回数据中所述,我正在为图像 blogPost.thumbnail = [bpDictionary objectForKey:@"url"]; 使用正确的密钥:developer.espn.com/docs/headlines#what-it-returns>。我仔细检查以确保我将blogURL 更改为正确的链接,我肯定是。我不知道为什么当我更改它时,'blogPost.thumbnailURL' 会以 nil 的形式出现。

标签: ios objective-c json image espn


【解决方案1】:

问题是图片url不正确,显示为

“http://blog.teamtreehouse.com/wp-content/uploads/2014/05/pencils-wikimedia-150x150.jpg”

您应该必须从链接“/”中删除黑斜线, 从服务器加载到 imageView。

删除后检查这个 Check This http://blog.teamtreehouse.com/wp-content/uploads/2014/05/pencils-wikimedia-150x150.jpg

要以编程方式删除“\”,请使用此

NSString *str = @"http:\/\/blog.teamtreehouse.com\/wp-content\/uploads\/2014\/05\/pencils-wikimedia-150x150.jpg"
NSString *stringWithoutSlash = [str 
   stringByReplacingOccurrencesOfString:@"\" withString:@""];

【讨论】:

  • 不,但由于某种原因,这些链接以我的问题中列出的格式工作。 ESPN 网址由于某种原因无法正常工作。
  • 您可以通过编程方式从字符串中删除“\”。
  • 这甚至不是问题。问题是我没有从网址正确加载图像,例如a.espncdn.com/media/motion/2014/0501/…
【解决方案2】:

请尝试:

NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:blogPost.thumbnail]];

在您的 CellForRowAtIndexPath 中。如果这不起作用,请尝试检查 blogPost.thumbnail 中的内容。如果它是一个数组或字典,则需要将其拆分,直到获得要使用的 URL。

我不知道 ESPN Jsonstring 的样子,但如果你可以将 URL 分隔成一个字符串,它与上面的相同。

希望对你有帮助

【讨论】:

  • 这个问题中显示了一个 ESPN JSON(在第二段代码中):stackoverflow.com/questions/19491657/... 我想获取“图像”字典下的“url”对象。 @Amset –
  • 如何拆分“图片”以获得“网址”?你能告诉我代码吗?这是我到目前为止所拥有的,但它不起作用:BlogPost *blogPost = [BlogPost blogPostWithTitle:[bpDictionary objectForKey:@"headline"]]; blogPost.author = [bpDictionary objectForKey:@"linkText"]; NSDictionary *imagesDictionary = [bpDictionary objectForKey:@"images"]; blogPost.thumbnail = [imagesDictionary objectForKey:@"url"];
  • {} 包含您写入 NSDictionary 类的 JSON 对象。 [] 将您写入 NSArray 类的内容标记为数组。键“图像”包含一个数组。您想使用该数组的第一个条目。 blogPost.thumbnail = [[imagesDictionary objectAtIndex:0] objectForKey:@"url"];
  • 我试过了,但我收到一条错误消息,上面写着No visible @interface for 'NSDictionary' declares the selector 'objectAtIndex:'
  • 啊,是的,我在那里犯了一个错误......它不可能将 objectAtIndex 用于 NSDictionary,因此您需要将数组“images”放入 NSArray 对象中,然后您可以选择第一个条目通过使用 objectAtIndey 在该数组中。 NSArray *imagesArray = [NSArray alloc] initWithArray:[bpDictionary objectForKey:@"images"]; 然后blogPost.thumbnail = [[imagesArray objectAtIndex:0] objectForKey:@"url"];
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-18
  • 2014-05-05
  • 2018-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多