我试图弄清楚如何让MWphotobrowser从一个外部服务器的json文件中获取照片、图片标题、照片缩略图等等。
在viewDidLoad中,我有以下代码:
- (void)viewDidLoad {
NSURL *url = [NSURL URLWithString:@"https://s3.amazonaws.com/mobile-makers-lib/superheroes.json"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[super viewDidLoad];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)
{
NSLog(@"Back from the web");
}];
NSLog(@"Just made the web call");
}在Case3 MWphotobrowser的Menu.m中,我有以下代码:
case 3: {
photo.caption = [self.result valueForKeyPath:@"name"];
NSArray * photoURLs = [self.result valueForKeyPath:@"avatar_url"];
NSString * imageURL = [photoURLs objectAtIndex:indexPath.row];
[photos addObject:[MWPhoto photoWithURL:[NSURL URLWithString:imageURL]]];
enableGrid = NO;
break;
}如果您错过了,我使用的JSON文件是https://s3.amazonaws.com/mobile-makers-lib/superheroes.json
我做的任何事似乎都不起作用,有什么办法可以解决吗?
发布于 2014-09-06 07:10:09
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)
{
// here make sure ur response is getting or not
if ([data length] >0 && connectionError == nil)
{
// DO YOUR WORK HERE
self.superheroes = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error: &connectionError];
NSLog(@"data downloaded.==%@", self.superheroes);
}
else if ([data length] == 0 && connectionError == nil)
{
NSLog(@"Nothing was downloaded.");
}
else if (connectionError != nil){
NSLog(@"Error = %@", connectionError);
}
}];在这里,您从服务器获得的响应是NSArray -->NSMutableDictionary`
Case场景是
case 3: {
NSDictionary *superhero = [self.superheroes objectAtIndex:indexPath.row];
photo.caption = superhero[@"name"];
NSString * imageURL = superhero[@"avatar_url"];
// NSArray * photoURLs = superhero[@"avatar_url"];
[photos addObject:[MWPhoto photoWithURL:[NSURL URLWithString:imageURL]]];
enableGrid = NO;
break;
}你的最终出局是

https://stackoverflow.com/questions/25697082
复制相似问题