2014-02-14 42 views
0

我的代碼從url中加載7張圖片並將其數據添加到數組中。在這個過程結束時,我得到了一個8個對象數組,但我試圖顯示一個進度條,直到完成加載所有照片的過程。Progress View和背景加載

我沒有一個想法如何做到這一點...

這裏是代碼

-(void)SetUpDrinks 
{ 
    loadingView.hidden=NO; 
    [loadingView_activity startAnimating]; 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ 
     [UIApplication sharedApplication].networkActivityIndicatorVisible=YES; 
     imgsDATA = [[NSMutableArray alloc] init]; 
     for (int i=0; i<8; i++) { 
      imageDownloadNum++; 
      absPath = [NSString stringWithFormat:@"http://domain/app/menu/drinks/%i.png",imageDownloadNum]; 
      trimmedAbsPath = [absPath stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
      NSURL *imgURL = [NSURL URLWithString:trimmedAbsPath]; 
      NSLog(@"%@",imgURL); 
      imgDATA = [[NSData alloc] initWithContentsOfURL:imgURL]; 
      [imgsDATA addObject:imgDATA]; 
     } 

     dispatch_async(dispatch_get_main_queue(), ^{ 
      loadingView.hidden=YES; 
      [loadingView_activity stopAnimating]; 
      [UIApplication sharedApplication].networkActivityIndicatorVisible=NO; 
      [self RefreshImg]; 
     }); 

    }); 

} 
+0

請查看MBProgressHud https://github.com/jdg/MBProgressHUD – Jack

+0

如何使用進度組件?如果你在可可控制中進行搜索,有很多。 – alexandresoli

+0

將嘗試「MBProgressHud」看起來不錯。 – Ompel

回答

0

你可以做一個NSOperation子類來下載你的圖像,然後使用dispatch_group。調度組是阻止線程的一種方式,直到一個或多個任務完成執行 - 在這種情況下,我們正在等待所有下載完成。

您現在可以使用progressBlock更新用戶界面,並讓用戶知道有多少圖片已完成下載。

如果您需要單獨下載的進度,請參閱NSURLConnection參考。特別- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

- (void)enqueueGroupOfOperations:(NSArray *)operations 
         progress:(void (^)(NSUInteger completedCount, NSUInteger totalOperations))progressBlock 
         completion:(void (^)(NSArray *operations))completionBlock; 
{ 

    NSParameterAssert(operations); 
    NSParameterAssert(progress); 
    NSParameterAssert(completion); 

    __block dispatch_group_t group = dispatch_group_create(); 
    NSBlockOperation *dependentOperation = [NSBlockOperation blockOperationWithBlock:^{ 
     dispatch_group_notify(group, dispatch_get_main_queue(), ^{ 
      completion(operations); 
     }); 
     dispatch_release(group); 
    }]; 

    for (NSOperation *operation in operations) { 

     operation.completionBlock = ^{ 
      dispatch_group_async(group, dispatch_get_main_queue(), ^{ 

       NSUInteger count = [[operations indexesOfObjectsPassingTest:^BOOL(NSOperation *operation, NSUInteger idx, BOOL *stop) { 
        return [operation isFinished]; 
       }] count]; 

       progress(count, [operations count]); 

       dispatch_group_leave(group); 
      }); 
     }; 

     dispatch_group_enter(group); 
     [dependentOperation addDependency:operation]; 
    } 

    [self.operationQueue addOperations:operations waitUntilFinished:NO]; 
    [self.operationQueue addOperation:dependentOperation]; 
} 

如果這是你太多,那麼你可以去到哪裏AFNetworking這一切爲你做,https://github.com/AFNetworking/AFNetworking。但它總是很高興知道這些東西是如何工作的。