Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift IOS:如何使用NSURLSession和didReciveData下载图像?_Ios_Swift_Nsurlsession - Fatal编程技术网

Swift IOS:如何使用NSURLSession和didReciveData下载图像?

Swift IOS:如何使用NSURLSession和didReciveData下载图像?,ios,swift,nsurlsession,Ios,Swift,Nsurlsession,我是IOS开发的新手。你能告诉我如何使用NSURLSession和DidReceiveData方法下载图像吗?我需要一个进度视图与上传我的图像的进度。创建NSUrlSession后我卡住了。请帮忙 class ViewController: UIViewController, NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDataDelegate { @IBOutlet weak var prog

我是IOS开发的新手。你能告诉我如何使用NSURLSession和DidReceiveData方法下载图像吗?我需要一个进度视图与上传我的图像的进度。创建NSUrlSession后我卡住了。请帮忙

    class ViewController: UIViewController, NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDataDelegate {

        @IBOutlet weak var progressLabel: UILabel!
        @IBOutlet weak var imageView: UIImageView!
        @IBOutlet weak var progressView: UIProgressView!
        @IBOutlet weak var downloadButton: UIButton!

        @IBAction func downloadImage(sender: UIButton) {
            let urlString = "https://img-fotki.yandex.ru/get/6111/8955119.3/0_7e1f6_a73b98a0_orig"
            let url = NSURL(string: urlString)
           var configuration: NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
     var session: NSURLSession = NSURLSession(configuration: self.configuration)
        }

        func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData) {
            print("didReceiveData")
        }

        func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveResponse response: NSURLResponse, completionHandler: (NSURLSessionResponseDisposition) -> Void) {
            print("didReceiveRes")

        }

        func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {
            let alert = UIAlertController(title: "Alert", message: error?.localizedDescription, preferredStyle: .Alert)
            let alertAction = UIAlertAction(title: "Ok", style: .Default, handler: nil)
            alert.addAction(alertAction)
            presentViewController(alert, animated: true, completion: nil)
        }

        func URLSession(session: NSURLSession, task: NSURLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) {
             print("didReceiveSendData64")
            var uploadProgress: Float = Float(totalBytesSent) / Float(totalBytesExpectedToSend)
            progressView.progress = uploadProgress

        }



}
帮助完整教程:-

监控下载进度

func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {

// 1
if let downloadUrl = downloadTask.originalRequest?.URL?.absoluteString,
  download = activeDownloads[downloadUrl] {
  // 2
  download.progress = Float(totalBytesWritten)/Float(totalBytesExpectedToWrite)
  // 3
  let totalSize = NSByteCountFormatter.stringFromByteCount(totalBytesExpectedToWrite, countStyle: NSByteCountFormatterCountStyle.Binary)
  // 4
  if let trackIndex = trackIndexForDownloadTask(downloadTask), let trackCell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: trackIndex, inSection: 0)) as? TrackCell {
    dispatch_async(dispatch_get_main_queue(), {
      trackCell.progressView.progress = download.progress
      trackCell.progressLabel.text =  String(format: "%.1f%% of %@",  download.progress * 100, totalSize)
    })
}
  }
}

好吧,若你们对答案感到满意,那个么别忘了投票并接受答案。好的,但我不能投票,因为我的声誉还不到15。好的,我理解。谢谢你快乐的编码。