Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在ESPN JSON对象的表视图单元格中显示图像?_Ios_Objective C_Json_Image_Espn - Fatal编程技术网

Ios 如何在ESPN JSON对象的表视图单元格中显示图像?

Ios 如何在ESPN JSON对象的表视图单元格中显示图像?,ios,objective-c,json,image,espn,Ios,Objective C,Json,Image,Espn,我以前使用过JSON对象,比如树屋博客:。他们的缩略图列在如下字符串下:http:\/\/blog.teamtreehouse.com\/写作技巧更好的商业营销。当我使用此代码访问他们的数据时: - (void)viewDidLoad { [super viewDidLoad]; NSURL *blogURL = [NSURL URLWithString:@"http://blog.teamtreehouse.com/api/get_recent_summary/"];

我以前使用过JSON对象,比如树屋博客:。他们的缩略图列在如下字符串下:http:\/\/blog.teamtreehouse.com\/写作技巧更好的商业营销。当我使用此代码访问他们的数据时:

- (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中显示其图像,例如:

我可以正确地访问他们的标题和作者数据,但当我试图访问他们的图像数据时,我什么也得不到。我注意到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;
}
)

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

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

您必须从链接“/”中删除黑色斜杠, 从服务器加载到imageView

移除后,请检查此项

要以编程方式删除“\”,请使用以下命令

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

NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:blogPost.thumbnail]];
在你的牢房里。如果这不起作用,试着检查blogPost.thumbnail中的内容。如果是数组或字典,则需要将其拆分,直到获得要使用的URL

我不知道ESPN Jsonstring看起来是什么样子,但是如果你能把URL分成一个字符串,它和上面的一样


希望它有帮助

如果问题是加载图像,请显示该代码而不是您发布的内容。该代码会发生什么?日志blogPost.thumbnailURL。imageData是否为非零?您需要提供详细信息,以便我们提供帮助。当我记录blogPost.thumbnailURL时,我会得到以下数组:2014-05-02 01:44:10.176 BlogReader[9081:60b]null如果该值为零,则无法加载任何内容。看起来你没有从JSON中正确获取图像URL。明白了。我正在为图像blogPost.thumbnail=[bpDictionary objectForKey:@url]使用正确的键;如ESPN的API返回数据所述:。我仔细检查了一下,以确保我正在将blogURL更改为正确的链接,我肯定是这样。我不知道为什么当我更改“blogPost.thumbnailURL”时,它会以零的形式出现在我们面前。不知道,但出于某种原因,这些链接会以我问题中列出的格式工作。ESPN url由于某种原因无法正常工作。您可以通过编程从字符串中删除“\”。这甚至不是问题所在。问题是,我没有正确地从url加载图像,例如,在第二段代码中的问题中显示了ESPN JSON:stackoverflow.com/questions/19491657/…我想在图像字典下获取url对象@Amset–如何分割图像以获取url?你能告诉我密码吗?这是我到目前为止所拥有的,但它不起作用:BlogPost*BlogPost=[BlogPost blogPostWithTitle:[bpDictionary objectForKey:@headline];blogPost.author=[bpDictionary objectForKey:@linkText];NSDictionary*imagesDictionary=[bpDictionary objectForKey:@images];blogPost.缩略图=[imagesDictionary objectForKey:@url];{}包含一个JSON对象,该对象是您写入NSDictionary类的对象。[]将写入NSArray类的内容标记为数组。关键图像包含一个数组。您希望使用该数组的第一个条目。blogPost.thumboil=[[imagesDictionary objectAtIndex:0]objectForKey:@url];我试过了,但我收到一条错误消息,上面说“NSDictionary”没有可见的@interface声明选择器“objectAtIndex:”啊,是的,我在那里犯了一个错误。。。不可能对NSDictionary使用objectAtIndex,因此需要将数组图像放入NSArray对象中,然后可以使用objectAtIndey拾取该数组中的第一个条目。NSArray*imagesArray=[NSArray alloc]initWithArray:[bpDictionary objectForKey:@images];然后blogPost.thumboil=[[imagesArray objectAtIndex:0]objectForKey:@url];