NSURLConnection,在ios9.0以后被废弃,以后使用URLSession类,如下图
具体样例:
self.imageV.frame = CGRect(x:20,y:70,kScreenWidth-40,height:200) self.view.addSubview(self.imageV) //使用URLSession加载图片 let url = URL(string:"http://pic.jj20.com/up/allimg/911/021616153629/160216153629-1.jpg") //创建请求对象 let request = URLRequest(url:url!) let session = URLSession.shared let dataTask = session.dataTask(with: request,completionHandler: { (data, response, error) -> Void in if error != nil{ print(error.debugDescription) }else{ //将图片数据赋予UIImage let img = UIImage(data:data!) self.imageV.image = img } }) as URLSessionTask //启动任务 dataTask.resume()
效果如下: