Objective-c:如何从DATA URL接收缩略图?

时间:2013-10-06 19:53:48

标签: ios objective-c youtube-api gdata

首先,我有这个gdata网址,它获取了一个gdata feed:

https://gdata.youtube.com/feeds/api/videos/tge2BfiIXiE?v=2&alt=jsonc

这是从网址获取信息的代码:

NSURL *feedURL = [NSURL URLWithString:@"https://gdata.youtube.com/feeds/api/videos/tge2BfiIXiE?v=2&alt=jsonc"];

NSData *jsonData = [NSData dataWithContentsOfURL:feedURL];

NSError *error = nil;

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

NSDictionary *data1 = [dataDictionary objectForKey:@"data"];    
NSString *t  = [data1 objectForKey:@"title"];
NSLog(@"Title:%@", t);

NSDictionary *thumbs = [dataDictionary objectForKey:@"thumbnail"]; 

NSURL *standardThumb  = [thumbs objectForKey:@"sqDefault"];
NSURL *hdThumb  = [thumbs objectForKey:@"hqDefault"];

上面的代码获取视频的标题。但是,代码无法从gdata URL成功获取缩略图。当我尝试NSLOG hdThumb网址时,我只会收到null个回复,所以我的问题是如何解决这个问题。

1 个答案:

答案 0 :(得分:1)

试试这个:

NSDictionary *thumbs = [[dataDictionary objectForKey:@"data"] objectForKey:@"thumbnail"];

或者您可以使用此语法来访问字典值:

NSDictionary *thumbs = dataDictionary[@"data"][@"thumbnail"];

你的问题是你忘了访问"数据"字典第一