0

购买完成后,我想从服务器下载图像、音频和视频。

目前我能够下载图像并可以在 UIProgressView 中看到进度,但我不确定如何为多个 url 请求显示单个 UIProgressView。

我想知道我们是否可以使用 ASIHttpRequest 为多个 url 请求显示一个 UIProgressView。

请让我知道如何进行?非常感谢

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.request=nil;
    }
    return self;
}

- (void)fetchThreeImages1:(id)sender
{
    [imageView1 setImage:nil];

    if (!networkQueue) {
        networkQueue = [[ASINetworkQueue alloc] init];  
    }
    failed = NO;
    [networkQueue reset];
    [networkQueue setRequestDidFinishSelector:@selector(requestForDownloadOfFileFinished:)];
    [networkQueue setRequestDidFailSelector:@selector(requestForDownloadOfFileFailed:)];



    [networkQueue setShowAccurateProgress:YES];
    [networkQueue setDelegate:self];
    self.request=nil;

    NSURL *url;

    NSString *urlString=@"http://www.digitalreview.ca/cams/pics/DSCN0044.JPG";
    url = [NSURL URLWithString:urlString];
    request = [ASIHTTPRequest requestWithURL:url];
    NSString *Filename = [urlString lastPathComponent];

    [request setDownloadProgressDelegate:imageProgressIndicator1];
    [request setUserInfo:[NSDictionary dictionaryWithObject:@"request1" forKey:@"name"]];
    [request setDownloadDestinationPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:Filename]];
    [request setShouldContinueWhenAppEntersBackground:YES];
    [request setDelegate:self];
    [request setDidReceiveDataSelector:@selector(request:didReceiveBytes:)]; 
    [request setShowAccurateProgress:YES];

    [networkQueue addOperation:request];


    [networkQueue go];

}
- (void)requestForDownloadOfFileFinished:(ASIHTTPRequest *)request1
{

    NSLog(@"req finish.........");
    NSLog(@"Content will be %llu bytes in size",[request1 contentLength]); 

    goButton.hidden=YES;
    [imageProgressIndicator1 removeFromSuperview];
    UIImage *img = [UIImage imageWithContentsOfFile:[request1 downloadDestinationPath]];
    array=[[[NSMutableArray alloc]init]autorelease];
    [array addObject:img];
    image = [[UIImage alloc ]initWithData:[request1 responseData]];


    NSString *receivedString = [request1 responseString];
    NSLog(@"received string %@",receivedString);
    NSData *responseData = [request1 responseData];

    NSString *response = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

    NSLog(@"Server response:%@", response);

    NSLog(@"response data %@",[request1 responseData]);
    NSLog(@"download destination path %@",[request downloadDestinationPath]);
    NSLog(@"download destination path1 %@",[request1 downloadDestinationPath]);
    NSLog(@"image %@",img);
    NSLog(@"image1 %@",image);
    if (img) {
        [imageView1 setImage:img];
    }
    UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"Download" message:@"Download Completed" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
    [alertView show];
    //completed=true;
    NSLog(@"mutablearray count %@",[array objectAtIndex:0]);


}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
 (NSIndexPath *)indexPath
 //-------------------------------------------------------------------------------
 {
   int tablePadding = 40;
   int tableWidth = [self.tblViewDownload frame].size.width;
   if (tableWidth > 480) {
     tablePadding = 110;
   }

  static NSString *CellIdentifier = @"TypeCell";

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

  if (cell == nil)
  {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero]autorelease];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    if([indexPath row]==0)
    {
      NSString *urlString=@"http://www.digitalreview.ca/cams/pics/DSCN0044.JPG";

      NSString* theFileName = [urlString lastPathComponent];

      NSLog(@"%@ the filename",theFileName);

      goButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
      [goButton setTitle:@"Go" forState:UIControlStateNormal];
      [goButton sizeToFit];
      [goButton setFrame:CGRectMake(220,30,50,30)];

      [goButton addTarget:self action:@selector(fetchThreeImages1:) forControlEvents:UIControlEventTouchUpInside];
      [cell addSubview:goButton];

    //-------------
    NSString *workSpacePath=[[self applicationDocumentsDirectory] stringByAppendingPathComponent:theFileName];
    NSLog(@"%@ workSpacePath ",workSpacePath);
    if ( workSpacePath ){

        NSLog(@"%@ workSpacePath ",workSpacePath);

        //--------------
        UIImage *imgBack = [UIImage imageNamed:@"btn-back.png"];
        imageView1 = [[[UIImageView alloc] initWithFrame:CGRectMake(0,0,20,20)] autorelease];
        [imageView1 setBackgroundColor:[UIColor grayColor]];

        //imageView1.image = [UIImage imageWithData:[NSData dataWithContentsOfFile:workSpacePath]];
        imageView1.image = [UIImage imageWithContentsOfFile:workSpacePath];
        [cell addSubview:imageView1];

        NSLog(@"dfdfD");
        NSLog(@"sdfdsf");
    }



    imageProgressIndicator1 = [[[UIProgressView alloc] initWithFrame:CGRectZero] autorelease];
    [cell addSubview:imageProgressIndicator1]; 

    }
    NSUInteger imageWidth = (tableWidth-tablePadding-20)/3;
    NSUInteger imageHeight = 35;
   [imageView1 setFrame:CGRectMake(tablePadding/2,20,imageWidth,imageHeight)];
   [imageProgressIndicator1 setFrame:CGRectMake(120,40,imageWidth,20)];

  }

  return cell;
}
4

1 回答 1

1

查看 ASIHTTPRequest 文档部分“跟踪一组请求的下载进度”

您基本上需要创建一个ASINetworkQueue并将您的每个请求添加到它。队列负责跟踪整体进度并将调用委托方法,您可以在其中更新进度条。

于 2012-07-04T11:26:09.700 回答