Как отобразить изображения в ячейке табличного представления из объекта ESPN JSON?

Ранее я работал с объектами JSON, например, для блога TreeHouse: http://blog.teamtreehouse.com/api/get_recent_summary/. Их миниатюрные изображения перечислены в виде строк: "http:\/\/blog.teamtreehouse.com\/writing-tips-лучше-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. Но я пытаюсь использовать API ESPN для создания приложения ESPN Blog Reader. Они отображают свои изображения в 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 содержатся изображения \ "\ / \ /", а в URL-адресе изображения ESPN этого нет. Как получить доступ к данным изображения для каждого объекта JSON, используя формат URL ESPN?

Здесь я загружаю и устанавливаю изображения:

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

2 ответа

Пожалуйста, попробуй:

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

В вашем CellForRowAtIndexPath. Если это не работает, попробуйте проверить, что в blogPost.thumbnail. Если это массив или словарь, вам нужно разделить его, пока не получите URL, который вы хотите использовать.

Я не знаю, как выглядит ESPN Jsonstring, но если вы можете разделить URL-адрес в строке, то же самое, что и выше.

Надеюсь, поможет

Проблема в том, что URL-адрес изображения не правильный, он отображается как

" 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:@""];
Другие вопросы по тегам