【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{            }        }    }
原创粉丝点击