2016-05-13 71 views
1

我正在嘗試在iOS上學習Web服務。Objective-C:從JSON獲取圖像

我開始從JSON API鏈接獲取圖像。

我用下面的代碼,但圖像不顯示,我在收到警告,說

不兼容的指針類型從「NSSting * _Nullable」

分配給「的UIImage * _Nullable」

我的代碼

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

,如果你使用的是像sendAsynchronousRequest塊操作,你應該把你的代碼是依賴於塊的結果裏面塊 –

回答

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(), ^{ 

     }); 
    } 
    }); 

    } 
}]; 

完美的方式來顯示圖像!

希望這將有助於你:)

2

你需要在你從webservice得到響應後在塊內編寫你的代碼。

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

感謝這一點,但仍然沒有顯示。 – Ramiro

+0

檢查您的popUpAdURL,其即將到來 –

+0

popUpAdURL是圖片點擊時的鏈接。 – Ramiro

0

首先,檢查瀏覽器中是否有該網址。如果是,請檢查您的應用是否接受HTTP(不是HTTPS)。你可以像這樣設置應用接受: enter image description here

+0

這樣做沒有一個很好的理由會讓你的應用程序被拒絕。顯然它會破壞你的應用程序的安全性。 – gnasher729

+0

@ gnasher729但爲什麼我的不是? – Lumialxk

1

如果你需要處理來自web響應的圖像,那麼你可以使用來自GitHub的SDWebImage庫。

在閱讀我的頁面時,他們也提供瞭如何實現它的方式。

#import <SDWebImage/UIImageView+WebCache.h> 

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

那就是它!

0

從服務器獲取圖像後,您在view中添加popUpBanner。所以你必須在viewdidload中初始化它,並且需要設置它的框架。

然後從主線程上web服務的完成處理程序中將圖像設置爲popUpBanner。並確保你必須設置圖像而不是圖像名稱字符串!

下面的代碼是錯誤的,它試圖將字符串設置爲imageview,這是不可能的。從image url

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

獲取圖像由服務方和使用圖像名稱您在響應得到提供。

並確保您使用斷點獲取圖像對象。

希望這將有助於:)