【Swift】下载图片
来源:互联网 发布:linux怎样备份数据库 编辑:程序博客网 时间:2024/05/17 20:53
/// 下载请求对象 var downloadRequest: Request let urlString = FilePath.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)! downloadRequest = Alamofire.download(.GET, urlString, destination: destination) self.downloadRequest.progress(downloadProgress) self.downloadRequest.response(completionHandler: downloadResponse)
//下载过程中改变进度条 func downloadProgress(bytesRead: Int64, totalBytesRead: Int64, totalBytesExpectedToRead: Int64) {// let percent = Float(totalBytesRead)/Float(totalBytesExpectedToRead) print("\(Float(totalBytesRead))----\(Float(totalBytesExpectedToRead))") // 进度条更新 dispatch_async(dispatch_get_main_queue(), {// self.progress.setProgress(percent,animated:true) self.downView.changeDownLoadProgrees(Float(totalBytesRead), totalBytesExpectedToRead: Float(totalBytesExpectedToRead)) })// print("当前进度:\(percent*100)%") } //下载停止响应(不管成功或者失败) func downloadResponse(request: NSURLRequest?, response: NSHTTPURLResponse?, data: NSData?, error:NSError?) { if let error = error { if error.code == NSURLErrorCancelled { self.cancelledData = data //意外终止的话,把已下载的数据储存起来 } else { print("Failed to download file: \(response) \(error)") } } else {// let filePath:String = NSHomeDirectory() + "/Documents/QQ7.9.exe"// print("Successfully downloaded file: \(response)") downLoadSuccess() } }
/** 点击暂停还是点击继续 - parameter isStop: ture 暂停 ,false 继续 */ func actionStopOrGoWith(isStop: Bool) { if(isStop){ self.downloadRequest?.cancel() }else{ if let cancelledData = self.cancelledData { self.downloadRequest = Alamofire.download(resumeData: cancelledData, destination: destination) self.downloadRequest.progress(downloadProgress) //下载进度 self.downloadRequest.response(completionHandler: downloadResponse) //下载停止响应 }else{ } } }
阅读全文
0 0
- 【Swift】下载图片
- swift详解之十四 -----------NSThread 异步下载图片
- iOS后台下载图片并实现本地通知(Swift)
- swift 利用Opration和OprationQueue来下载网络图片
- swift 使用Kingfisher进行网络图片的下载
- swift 网络----利用URLSession的Data Task下载单张图片(包含图片的增量下载)
- Swift 系统学习 34 线程通信 Swift3.0 子线程下载图片主线程显示图片
- swift 网络----利用URLSession的Block模式下载多张图片
- swift 网络----利用URLSession的Download Task模式下载多张图片
- NSURLSession 下载文件 --Swift
- swift 加载网络图片
- swift 改变图片颜色
- swift:无限图片轮播器
- swift之图片浏览器
- Swift 图片下拉放大
- 【Swift】上传图片-NSURLSession
- Swift 图片轮播图
- Swift 图片平铺
- httpwebrequest下载文件失败的解决方案
- java-动态代理分析
- android 使用TextInputLayout创建一个登陆界面
- string和stringstream类
- Java基础部分第十八节
- 【Swift】下载图片
- 计算机网络-10
- CentOS修改hosts文件
- Java算法之选择排序
- Sublime Text 2支持GB2312和GBK,解决中文显示乱码问题
- 偏差和方差与过拟合欠拟合的关系
- Java基础部分第十九节
- 完整的SEO团队应该包括哪些人员(细分八要职)
- VHDL的port map映射时出现1166052WARNING