2016-07-08 3 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

알겠습니다. 행복한 코딩 감사합니다. –

관련 문제