2014-09-06 99 views
1

我想弄清楚如何獲取MWphotobrowser從json文件獲取照片,照片標題,照片縮略圖等等外部服務器。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

沒有我調整似乎使它的工作,任何想法如何解決這個問題?

+0

的結果是什麼你進入self.result – 2014-09-06 05:54:13

+0

其實你的結果來自[array {字典}]格式,那是我問 – 2014-09-06 05:56:33

+0

是的。在Menu.h中,我有@property(nonatomic,strong)NSMutableDictionary *結果;錯誤? – HalesEnchanted 2014-09-06 06:11:17

回答

0
[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; 

    } 

烏爾最後出來放是

enter image description here

+0

有一個快樂的一天, – 2014-09-06 07:14:51