2016-10-10 87 views
0

我想使用下面的代碼在swift中顯示來自URL的圖像,但沒有顯示任何內容。來自URL的Swift圖像

let url = NSURL(string: "https://www.psychicliving.co.uk/images/Michael.jpg") 
if let data = NSData(contentsOf: url as! URL) { 
    cell.imgCarNane.image = UIImage(data: data as Data) 
} 

奇怪的是,如果我代替圖像託管在不同的網站爲JPEG,如:

https://www.thawte.com/assets/hp/images/promo_myths.jpg

它顯示的罰款。

有沒有關於我想要使用的圖像會導致問題?

如果有人可以看一看,我會非常感激。

謝謝!

回答

0

在info.plist中

NSAllowsArbitraryLoads添加NSAppTransportSecurity = YES

let url = URL(string: "https://www.psychicliving.co.uk/images/Michael.jpg") 
    let data = try? Data(contentsOf: url!) //make sure your image in this url does exist, otherwise unwrap in a if let check/try-catch 
    imageViewTest.image = UIImage(data: data!) 
}