我正在嘗試在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];
,如果你使用的是像sendAsynchronousRequest塊操作,你應該把你的代碼是依賴於塊的結果裏面塊 –