进度视图和后台加载

时间:2014-02-14 17:44:47

标签: ios objective-c uiview

我的代码正在从url加载7个图片并将其数据添加到数组中。在这个过程的最后我得到了一个8个对象的数组,但是我试图显示一个进度条,直到加载所有照片的过程完成。

我不知道该怎么做......

这是代码

-(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];
        });

    });

}

1 个答案:

答案 0 :(得分: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。但总是很高兴知道这些东西是如何起作用的。