2016-07-08 11 views
2

私はIOS開発の初心者です。 NSURLSessionとdidReciveDataメソッドを使用してイメージをダウンロードする方法を教えてもらえますか?私は自分のイメージをアップロードする進捗状況を見るために進捗状況が必要です。私はNSUrlSessionを作成した後に立ち往生しました。助けてください。Swift IOS:NSURLSessionとdidReciveDataを使用してイメージをダウンロードするには?

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 

     } 



} 

答えて

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

ウェルカム、あなたは答えに満足しては、投票を忘れてしまったとの答えを受け入れない場合。 –

+0

しかし、私の評判がまだ15未満であるので投票できません。 –

+0

私は理解します。ハッピーコーディングありがとうございます。 –

関連する問題