Swift - 异步加载图片

前端之家收集整理的这篇文章主要介绍了Swift - 异步加载图片前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
fun loadImage(imgView:ImageView,imgURL:NSURL) {
//定义NSURL
let imgURL: NSURL = NSURL (string: " http://hangge.com/blog/images/logo.png" )!;
//定义NSURLRequest
let request: NSURLRequest = NSURLRequest ( URL :imgURL);
//异步获取图片
NSURLConnection .sendAsynchronousRequest(request,queue: NSOperationQueue .mainQueue(),
completionHandler: {(response: NSURLResponse !,data: NSData !,error: NSError !)-> Void in
//将图片数据赋予UIImage
let img= UIImage (data:data);
imgView .image = img;
})
}

猜你在找的Swift相关文章