首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MWphotobrowser -从Json获取图像

MWphotobrowser -从Json获取图像
EN

Stack Overflow用户
提问于 2014-09-06 05:19:59
回答 1查看 222关注 0票数 1

我试图弄清楚如何让MWphotobrowser从一个外部服务器的json文件中获取照片、图片标题、照片缩略图等等。

在viewDidLoad中,我有以下代码:

代码语言:javascript
复制
- (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中,我有以下代码:

代码语言:javascript
复制
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

我做的任何事似乎都不起作用,有什么办法可以解决吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-06 07:10:09

代码语言:javascript
复制
[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场景是

代码语言:javascript
复制
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;

    }

你的最终出局是

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25697082

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档