我在网上找了许多加载图片的例子,但用swift实现加载图片遇到一个问题
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
}