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

时间:2016-07-08 09:41:29

标签: ios swift nsurlsession

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

conda skeleton pypi seasonal
conda build seasonal
conda install --use-local seasonal

1 个答案:

答案 0 :(得分:2)

帮助完整教程raywenderlich: -

监控下载进度

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)
    })
}
  }
}