user198725878
user198725878

Reputation: 6386

ASIHttprequest Muliple request with one progressview

After the purchase is completed ,i want to donwload image, audio and video from server.

Currently i am able to download the image and can see the progress in UIProgressView but i am not sure how can we show the single UIProgressView for multiple url requests.

I would like to know whether we can show one UIProgressView for multiple url request using ASIHttpRequest.

please let me know how to proceed? and thanks a lot

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

Upvotes: 0

Views: 823

Answers (1)

Robin Summerhill
Robin Summerhill

Reputation: 13675

Have a look at the ASIHTTPRequest documentation section 'Tracking download progress for a set of requests'.

You basically need to create an ASINetworkQueue and add each of your requests to it. The queue is responsible for tracking overall progress and will call delegate methods where you can update your progress bar.

Upvotes: 1

Related Questions