我在網(wǎng)上找了許多加載圖片的例子,但用swift實現(xiàn)加載圖片遇到一個問題
func downloadImage() -> Void {
let url = NSURL(string: "...圖片地址...")
let data = NSData(contentsOfURL: url!)
let image = UIImage(data: data!)
imageView.image = image
}
這段代碼中data會是nil猎贴,崩潰,根本無法完成加載
最后換了一段代碼,這樣子,加載成功
func downloadImage(){
let url = NSURL(string: "...圖片地址")
let data = try? NSData(contentsOfURL: url!, options: NSDataReadingOptions.DataReadingMapped)
let image = UIImage(data: data!)
imageView.image = image
}