2016-05-13 6 views
1

Ich versuche, Web-Service auf iOS zu lernen.Ziel-C: Bild von JSON

Ich fange an, ein Bild von einem JSON API Link zu bekommen.

Ich habe den Code unten verwendet, aber das Bild angezeigt wird nicht, und ich bin Warnung erhalten, die Typen die Zuordnung zu ‚UIImage * _Nullable‘ von ‚NSSting * _Nullable‘

Inkompatible Zeiger sagt

Mein Code

NSURL *urlAdPop = [NSURL URLWithString:@"JSON LINK HERE"]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:urlAdPop]; 
    [NSURLConnection sendAsynchronousRequest:request 
             queue:[NSOperationQueue mainQueue] 
          completionHandler:^(NSURLResponse *response, 
               NSData *data, NSError *connectionError) 
    { 
     if (data.length > 0 && connectionError == nil) 
     { 
      NSDictionary *AdPopUp = [NSJSONSerialization JSONObjectWithData:data 
                     options:0 
                     error:NULL]; 
      popUpBanner.image = [[AdPopUp objectForKey:@"ad_image"] stringValue]; 
      popUpAdURL = [AdPopUp objectForKey:@"ad_link"]; 
     } 
    }]; 



    popUpBanner.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:popUpAdURL]]; 
    popUpBanner.hidden = NO; 
    popUpBanner.layer.cornerRadius = 9; 
    popUpBanner.clipsToBounds = YES; 
    popUpBanner.userInteractionEnabled = YES; 
    [self.view addSubview:popUpBanner]; 
+0

:) hilft, wenn Sie Blockoperationen wie sendAsynchronousRequest verwenden, sollten Sie Ihren Code setzen, die für diese –

Antwort

3
popUpBanner.layer.cornerRadius = 9; 
popUpBanner.clipsToBounds = YES; 
popUpBanner.userInteractionEnabled = YES; 
popUpBanner.hidden = YES; 
[self.view addSubview:popUpBanner]; 

NSString* [email protected]"JSON LINK HERE"; 
NSString* webStringURL = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

NSURL *urlAdPop = [NSURL URLWithString:webStringURL]; 
NSURLRequest *request = [NSURLRequest requestWithURL:urlAdPop]; 
[NSURLConnection sendAsynchronousRequest:request 
            queue:[NSOperationQueue mainQueue] 
         completionHandler:^(NSURLResponse *response, 
              NSData *data, NSError *connectionError) 
{ 
    if (data.length > 0 && connectionError == nil) 
    { 
     NSDictionary *AdPopUp = [NSJSONSerialization JSONObjectWithData:data 
                    options:0 
                    error:NULL]; 


     popUpAdURL = [AdPopUp objectForKey:@"ad_link"]; 
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{  
     NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[AdPopUp objectForKey:@"ad_image"]]]]; 
     if (imgData) 
      { 

      UIImage *image = [UIImage imageWithData:imgData]; 
      if (image) 
       { 
      dispatch_async(dispatch_get_main_queue(), ^{ 
       popUpBanner.image = image; 
       popUpBanner.hidden = NO; 

      }); 
     } 
     else 
     { 
      dispatch_async(dispatch_get_main_queue(), ^{ 

      }); 
     } 
    } 
    else 
    { 
     dispatch_async(dispatch_get_main_queue(), ^{ 

     }); 
    } 
    }); 

    } 
}]; 

Perfekte Weise, Bild anzuzeigen helfen!

hoffen, dass es Ihnen

2

Sie müssen Ihren Code innerhalb des Blocks schreiben, nachdem Sie eine Antwort vom Webservice bekommen haben.

NSURL *urlAdPop = [NSURL URLWithString:@"JSON LINK HERE"]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:urlAdPop]; 
    [NSURLConnection sendAsynchronousRequest:request 
             queue:[NSOperationQueue mainQueue] 
          completionHandler:^(NSURLResponse *response, 
               NSData *data, NSError *connectionError) 
    { 
     if (data.length > 0 && connectionError == nil) 
     { 
      NSDictionary *AdPopUp = [NSJSONSerialization JSONObjectWithData:data 
                    options:0 
                     error:NULL]; 
      popUpBanner.image = [[AdPopUp objectForKey:@"ad_image"] stringValue]; 
      popUpAdURL = [AdPopUp objectForKey:@"ad_link"]; 
       UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:popUpAdURL]]; 

      dispatch_async(dispatch_get_main_queue(), ^{ // get main thread to update image 
        popUpBanner.image= image 
        popUpBanner.hidden = NO; 
       popUpBanner.layer.cornerRadius = 9; 
       popUpBanner.clipsToBounds = YES; 
       popUpBanner.userInteractionEnabled = YES; 
       [self.view addSubview:popUpBanner]; 

      }); 
     } 
    }]; 
+0

Dank auf Ergebnis des Blocks nach innen Block abhängig sind, aber es gibt noch keine Anzeige. – Ramiro

+0

Überprüfen Sie Ihre popUpAdURL., Es kommt richtig –

+0

der PopUpAdURL ist ein Link, wenn das Bild angetippt wird. – Ramiro

0

Erstens, überprüfen Sie, ob diese URL im Browser verfügbar ist. Wenn ja, prüfen Sie, ob Ihre App HTTP akzeptiert (nicht HTTPS). Sie können App so einstellen: enter image description here

+0

Wenn Sie dies ohne guten Grund tun, wird Ihre App abgelehnt. Und natürlich zerstört es die Sicherheit Ihrer App. – gnasher729

+0

@ gnasher729 Aber warum meins nicht? – Lumialxk

1

Wenn Sie mit Bildern kommen aus Web-Antwort kommt dann können Sie SDWebImage Bibliothek von GitHub verwenden.

In seiner Readme-Seite haben sie auch die Art und Weise, wie Sie es erreichen können.

#import <SDWebImage/UIImageView+WebCache.h> 

[self.YourImageView sd_setImageWithURL:[NSURL URLWithString:@"http://yourimagePath/.../"] 
         placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 

Das ist es!

0

Sie fügen popUpBanner in view hinzu, nachdem das Image vom Server abgerufen wurde. also musst du es vorher in viewdidload initialisieren und musst seinen Frame setzen.

Setzen Sie dann Bild auf popUpBanner von Abschluss-Handler des Web-Service auf Haupt-Thread. Und stellen Sie sicher, dass Sie Bild nicht Bildname Zeichenfolge festlegen müssen!

Die Zeile unten ist falsch, es wird versucht, eine Zeichenfolge für die Bildansicht festzulegen, was nicht möglich ist.

popUpBanner.image = [[AdPopUp objectForKey:@"ad_image"] stringValue]; 

get Bild von image url zur Verfügung gestellt von Serviceseite und Verwendung Bildnamen, die Sie als Antwort erhalten.

Und stellen Sie sicher, dass Sie Bildobjekt mit Haltepunkt erhalten.

Hope this wird :)