1

여러 개의 .mp4 비디오를 다운로드하고 각각 progressBar를 표시해야합니다. 이러한 진도를 tableView에 표시해야합니다. 나는 첫 번째 방법을progressbar가있는 다중 (Paralllel) 파일 (.mp4)을 다운로드하여 갤러리에 저장하십시오.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
    NSLog(@"Downloading Started"); 

    NSString *urlToDownload = @"http://original.mp4"; 

    NSURL *url = [NSURL URLWithString:urlToDownload]; 

NSData *urlData = [NSData dataWithContentsOfURL:url]; 

if (urlData) 
{ 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 

    NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"thefile.mp4"]; 

//saving is done on main thread 
    dispatch_async(dispatch_get_main_queue(), ^{ 

      [urlData writeToFile:filePath atomically:YES]; 

      NSLog(@"File Saved !"); 
}); 
} 

}); 

..

는 현재이 코드를 사용하여 ... 하나의 비디오를 다운로드하고 사용하여 갤러리에 저장하는 방법을 알고하는 방법을 모든

  1. 먼저 알고 코드를 사용하여 다운로드하는 동안 진행 상황을 어떻게 표시 할 수 있습니까?
  2. 그럼 어디서 다운로드하는지도 모르겠습니다. 위 코드가 .mp4 비디오를 저장하고 수정 (갤러리에서 저장)하려는 경로를 알고 싶습니다.
  3. 각 비디오의 다운로드 진행 상황을 보여주고 싶습니다.

두 번째 방법

내가 너무 NSOperationQueue를 사용하여 비동기 적으로 다운로드를 실행, 특정 숫자는 등, 병렬로 수행 할 수 있도록해야한다 생각합니다. 하지만 진도와 함께 그것을 구현하는 방법을 모르겠다.

+0

병렬 다운로드 또는 연속 다운로드가 필요합니까? – souvickcse

+0

병렬 다운로드가 필요합니다. – Nepster

답변

0

나는 여러 작업을 다운로드해야만 무언가를 만들었고 각각의 진행 상황을 보여줍니다 : http://github.com/ManavGabhawala/CAEN-Lecture-Scraper (this is the mac) 기본적으로 필요한 것 할 일은 내부에 진행 막대가 있고 바에 IBOutlet이있는 사용자 정의 UITableViewCell 클래스를 만드는 것입니다. 그런 다음 셀 클래스를 NSURLSessionDownloadDelegate와 일치시켜 진행 상황을 확인합니다. 또한 세마포어를 사용하여 동시에 세 번의 다운로드가 발생했는지 확인합니다. 여기에 귀하의 편의를 위해 클래스의 :

let downloadsAllowed = dispatch_semaphore_create(3) 
class ProgressCellView: UITableViewCell 
{ 
    @IBOutlet var progressBar: UIProgressView! 
    @IBOutlet var statusLabel: UILabel! 
    var downloadURL : NSURL! 
    var saveToPath: NSURL! 

    func setup(downloadURL: NSURL, saveToPath: NSURL) 
    { 
     self.downloadURL = downloadURL 
     self.saveToPath = saveToPath 
     statusLabel.text = "Waiting To Queue" 
     // Optionally call begin download here if you don't want to wait after queueing all the cells. 
    } 

    func beginDownload() 
    { 
     statusLabel.text = "Queued" 
     let session = 
     NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration(), delegate: self, delegateQueue: NSOperationQueue()) 
     let task = session.downloadTaskWithURL(downloadURL)! 
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { 
      dispatch_semaphore_wait(downloadsAllowed, DISPATCH_TIME_FOREVER) 
      dispatch_async(dispatch_get_main_queue(), { 
       self.statusLabel.text = "Downloading" 
      }) 
      task.resume() 
     }) 
    } 
} 
extension ProgressCellView : NSURLSessionDownloadDelegate 
{ 
    func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) 
    { 
     progressBar.setProgress(Float(totalBytesWritten)/Float(totalBytesExpectedToWrite), animated: true) 
    } 
    func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) 
    { 
     // TODO: Write file downloaded. 
     // Copy file from `location` to `saveToPath` and handle errors as necessary. 
     dispatch_semaphore_signal(downloadsAllowed) 
     dispatch_async(dispatch_get_main_queue(), { 
      self.statusLabel.text = "Downloaded" 
     }) 
    } 
    func URLSession(session: NSURLSession, didBecomeInvalidWithError error: NSError?) 
    { 
     print(error!.localizedDescription) 
     // TODO: Do error handling. 
    } 
} 

변경 당신이 원하는 무엇이든에 세마포어 값의 창조 (동시 다운로드 수는 허용). 또한 인스턴스화 한 후에 downloadURL 및 saveToPath를 사용하여 셀에서 setup을 호출해야합니다. 즉시 대기열에 넣고 비디오를 다운로드하려면 beginDownload()setup() 내부로 전화하십시오. 그렇지 않으면 모든 비디오를 다운로드하기 시작할 때마다 각 셀에서 호출하십시오.