<强>详解迅速利用施工和OprationQueue来下载网络图片强>
<强> 1。基于施工封装的获取网络数据组件强>
进口的基础 进口UIKit 公共typealias OpreationClosure=((_数据:数据# 63;_错误:错误# 63;)→无效) 操作类LJOpreationManager: {/* * *下载用的url */公共var imageUrl:字符串# 63;/* * *定义闭包属性,可选类型 */公共var ljcallBackClosure: OpreationClosure& # 63; func initWitParamter (_ url:字符串,_回调:@escaping OpreationClosure)→LJOpreationManager { 如果url !=" " { 自我。imageUrl url=自我。ljcallBackClosure=回调 } 回归自我 }//马克:——开始 覆盖func start () { 打印(“开始ljManager法”) self.startRequest () } func startRequest () {//1,创建URL下载地址 让url:网址!URL=(字符串:self.imageUrl !);//2、创建请求对象 var urlRequest: urlRequest=urlRequest (url: url); urlRequest。httpMethod="得到" urlRequest。httpShouldUsePipelining=true;//不需要缓存//urlRequest。cachePolicy=.reloadIgnoringLocalCacheData//3、创建会话 让配置=URLSessionConfiguration.default 让会话=URLSession(配置:配置,委托:自我,delegateQueue: .main)//4、下载任务//2祝辞——URLSessionDataDelegate模式 让loadDataTask=会话。dataTask (: urlRequest)//5、启动任务 loadDataTask.resume () }//初始化一个数据,用来存储下载下来的数据 私人var _responseData: NSMutableData ! var responseData:中NSMutableData ! { 得到{ 如果_responseData==nil { _responseData=NSMutableData () } 返回_responseData } 集{ 自我。_responseData=newValue } } }//马克- URLSessionDataDelegate模式获取数据 扩展LJOpreationManager: URLSessionDataDelegate { @available (iOS 7.0, *) func urlSession(_会话:urlSession dataTask: URLSessionDataTask, didReceive回应:URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition)→Swift.Void) {//允许继续加载数据 completionHandler(让) } @available (iOS 7.0, *) func urlSession(_会话:urlSession dataTask: URLSessionDataTask, didReceive数据:数据) {//每次获取的数据在此拼装//打印(“数据……\(数据)”) self.responseData.append(数据) 让currentBytes:=浮动浮动(self.responseData.length) 让allTotalBytes:浮动=浮动((dataTask.response& # 63; .expectedContentLength) !) 让proValu:=浮动浮动(currentBytes/allTotalBytes) 打印(“URLSessionDataDelegate——下载进度:- - - - - - \ (proValu * 100) %”); } func urlSession(_会话:urlSession,任务:URLSessionTask, didCompleteWithError错误:错误# 63;) { 如果ljcallBackClosure !=nil,让数据=https://www.yisu.com/zixun/self.responseData { 弱var weakSelf: LJOpreationManager ?=自我 DispatchQueue.main.async { 打印(“URLSessionDataDelegate——数据下载完毕”)//将接收的数据结果回调到前台,用于进度展示 weakSelf& # 63; .ljcallBackClosure !(数据作为数据,nil) } } } } >之前<强> 2。基于OprationQueue封装的网络数据管理组件强>
进口的基础 类LJWebImageManager: NSObject {///共享管理器使用。 公共静态让共享=LJWebImageManager () 公共var ljquee=OperationQueue () 覆盖init () { 如果可用(iOS 8.0 *) # { self.ljquee。qualityOfService=.background 其他}{ } } 公共函数requestByUrl (_ url:字符串,_回调:@escaping OpreationClosure)→LJOpreationManager { 让操作=LJOpreationManager ()。initWitParamter (url,回调) ljquee.addOperation(操作) 返回操作 } } >之前<强> 3。此处下载一张图片强>
func setCellData (_ labelNameStr:字符串,imageUrlStr:字符串) { titleLabel。文本=labelNameStr字符串/*会议的代表模式下载图片或者数据*/_=LJWebImageManager.shared。requestByUrl (imageUrlStr{(数据、错误) 如果错误==nil,数据!=nil { 让newImage=用户界面图像(数据:数据!作为数据) DispatchQueue.main.async { 让titleImage=UIImageView(框架:CGRect中(x: 0 y: 5、宽度:40岁,身高:40)) titleImage。形象=newImage self.contentView.addSubview (titleImage) } } 其他的 { 打印(错误& # 63;& # 63;"") } }) }详解迅速利用施工和OprationQueue来下载网络图片