2016-03-14 62 views
1

_pageImages是nsmutableArry我存儲圖像的URL。NSData到uiimage返回null

_pageImages = [[NSMutableArray alloc]initWithObjects:@"https://api.url2png.com/v3/P4DE5D1C99D8EF/7bbb6e0d1b74fb0ae1d4f18b06320096/400x400/abc.com",@"https://api.url2png.com/v3/P4DE5D1C99D8EF/7bbb6e0d1b74fb0ae1d4f18b06320096/400x400/abc.com", nil]; 

想要在uiimageview中顯示此URL圖像。

NSURL *url = [NSURL URLWithString:[_pageImages objectAtIndex:i]]; 

NSData *data = [[NSData alloc] initWithContentsOfURL:url]; 

UIImage *tmpImage = [UIImage imageWithData:data]; 

_backgroundImageView.frame = CGRectMake(0 ,10, 320, 320); 

_backgroundImageView.image = tmpImage; 

    [self.view addSubview:_backgroundImageView]; 

我看到了很多例子,但我不能得到正確的解決方案。新的發展。幫助我..

+2

您是否檢查過您的數據不是零?因爲也許你只是忘記禁用目標域名的應用傳輸安全 (當我嘗試https://api.url2png.com/v3/P4DE5D1C99D8EF/7bbb6e0d1b74fb0ae1d4f18b06320096/400x400/abc.com我自己,我在瀏覽器中出現錯誤...) – CZ54

+0

是的如何使用返回NSError對象的方法(例如'initWithContentsOfURL:options:error:')。 – trojanfoe

+0

你可以在這裏找到http://stackoverflow.com/questions/30096806/load-uiimage-from-url-in-ios –

回答

0

的問題是在URL試試這個!

試着訪問您的圖片網址,我得到的全部是「未授權」。

運行此示例代碼,您的代碼,只是使用不同的圖像地址。 檢查正確的圖像地址,它應該按預期運行。

NSMutableArray *pageImages = [[NSMutableArray alloc]initWithObjects:@"http://findicons.com/files/icons/1636/file_icons_vs_3/128/url.png",@"http://findicons.com/files/icons/1636/file_icons_vs_3/128/pdf.png", nil]; 

NSURL *url = [NSURL URLWithString:[pageImages objectAtIndex:0]]; 

NSData *data = [[NSData alloc] initWithContentsOfURL:url]; 

UIImage *tmpImage = [UIImage imageWithData:data]; 

_depImage.frame = CGRectMake(0 ,10, 320, 320); 
_depImage.image = tmpImage; 
+0

我改變了網址,但仍然存在UIImage * tmpImage = [UIImage imageWithData:data]; ... tmpImage返回null。 – Saravananr

+0

_backgroundImageView.image = tmpImage;是空 – Saravananr

+0

thanx你這麼多... – Saravananr

0

通過圖像發送AsynchronousRequest downloading--

NSURL *url = [NSURL URLWithString:[_pageImages objectAtIndex:i]]; 

[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:url] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { 
    _backgroundImageView.image = [UIImage imageWithData:data]; 
}]; 
+0

如何使用異步通信來解決問題? – trojanfoe

+0

_backgroundImageView.image = tmpImage;爲空 – Saravananr

+0

@Saravananr高興地幫助你。 :) –

0

我們可以簡單地使用下面從URL

加載圖像數據
_backgroundImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLwithString:urlString]]]; 

沒有必要在這裏調用一個服務調用的。

希望它有幫助..