1

我以前使用过 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;
}
)
4

2 回答 2

0

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

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

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

删除后检查此 检查此 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:@""];
于 2014-05-02T05:33:36.450 回答
0

Please try:

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

In your CellForRowAtIndexPath. If this doesn't work try to check whats in blogPost.thumbnail. If its an array or dictionary you need to split it up until you get the URL you want to use.

I don't know how the ESPN Jsonstring looks like, but if you can seperate the URL in a string its the same as above.

Hope it helps

于 2014-05-02T14:57:45.490 回答