2016-04-14 20 views
1

NSURLSessionTask kullanarak 2 resim (birer birer) yüklemeye çalışıyorum.Birden çok dosya yükleme için bir ilerleme çubuğu

- (void)URLSession:(NSURLSession *)session 
       task:(NSURLSessionTask *)task 
    didSendBodyData:(int64_t)bytesSent 
    totalBytesSent:(int64_t)totalBytesSent 
totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend 
{ 
if (self.imageName1 != nil && self.imageName2 != nil) 
    { 
     float progress = (float)totalBytesSent/(float)totalBytesExpectedToSend; 
     if (progress != 1.00) 
     { 
      // Calculate total bytes to be uploaded or the split the progress bar in 2 halves 
     } 
    } 
    else if (self.imageName1 != nil && self.imageName2 == nil) 
    { 
     float progress = (float)totalBytesSent/(float)totalBytesExpectedToSend; 
     if (progress != 1.00) 
     [self.progressBar1 setProgress:progress animated:YES]; 
    } 
    else if (self.imageName2 != nil && self.imageName1 == nil) 
    { 
     float progress = (float)totalBytesSent/(float)totalBytesExpectedToSend; 
     if (progress != 1.00) 
     [self.progressBar2 setProgress:progress animated:YES]; 
    } 
} 

2 resim yüklemesi durumunda ilerlemeyi göstermek için tek bir ilerleme çubuğunu nasıl kullanabilirim?

cevap

1

En iyi yol, NSProgress numaralı telefonu kullanmaktır; bu, çocuğa NSProgress güncelleştirmelerini bir arada yüklemenizi sağlar.

  1. Yani bir üst NSProgress tanımlayın:

    @property (nonatomic, strong) NSProgress *parentProgress; 
    
  2. NSProgress oluşturun ve bunu gözlemlemek NSProgressView söyleyin:

    self.parentProgress = [NSProgress progressWithTotalUnitCount:2]; 
    self.parentProgressView.observedProgress = self.parentProgress; 
    

    NSProgress olduğunda, NSProgressView ait observedProgress kullanarak güncellendi, ilgili NSProgressView da otomatik olarak güncellenecektir.

    self.child1Progress = [NSProgress progressWithTotalUnitCount:totalBytes1 parent:self.parentProgress pendingUnitCount:1]; 
    

    ve Sonra

    self.child2Progress = [NSProgress progressWithTotalUnitCount:totalBytes2 parent:self.parentProgress pendingUnitCount:1]; 
    
  3. , bireysel ağ isteklerinin ilerledikçe, güncelleme:

  4. Ardından bireysel istekler için, güncellenecektir Tek bir çocuk NSProgress girdileri, örneğin oluşturmak şu ana kadarki toplam bayt sayısı ile ilgili NSProgress:

    self.child1Progress.completedUnitCount = countBytesThusFar1; 
    

bireysel çocuğun completedUnitCount güncellenmesi NSProgress nesneler otomatik olarak buna göre ilerleme görünümünü güncelleyecektir, bunu gözlemliyoruz, çünkü ebeveyn NSProgress nesnenin fractionCompleted güncelleyecektir.

Ebeveynlerin totalUnitCount'un, çocukların pendingUnitCount toplamına eşit olmasını sağlamanız yeterlidir.