var imageView = UIImageView() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. imageView.frame = CGRect(x: 20, y: 80, 330, height: 450) self.view.addSubview(imageView) let picPath = "https://imgsrc.baidu.com/forum/pic/item/741ccaedab64034f38facc9ca4c379310b551d5f.jpg" let picUrl = URL(string: picPath) //使用分离线程选择器方法,创建一个线程,用来执行下载图片的方法 let globalQueue = DispatchQueue.global() //创建一个异步方法,用来下载网络图片 globalQueue.async { //通过数据对象下载网络图片,并将下载后的二进制数据存储在常量中 let picData = try? Data(contentsOf: picUrl!) //将数据对象转换成图片对象 let picture = UIImage(data: picData!) //返回主线程,并在主线程中更新图片内容 DispatchQueue.main.sync { self.imageView.image = picture } } }