download file with UIProgressView


/ Published in: Objective C
Save to your folder(s)



Copy this code and paste it in your HTML
  1. - (IBAction)download:(id)sender
  2. {
  3. NSURL *url = [NSURL URLWithString:@"http://123.jpg"];
  4. NSURLRequest *theRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10.0];
  5. connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
  6. connection = nil;
  7. }
  8.  
  9. - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
  10. self.filesize = [NSNumber numberWithUnsignedInteger:[response expectedContentLength]];
  11.  
  12. }
  13.  
  14. - (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)recievedData {
  15. if (data==nil) {
  16. data = [[NSMutableData alloc] initWithCapacity:2048];
  17. }
  18. [data appendData:recievedData];
  19. NSNumber *resourceLength = [NSNumber numberWithUnsignedInteger:[data length]]; //MAGIC
  20. float progress = [resourceLength floatValue] / [self.filesize floatValue];
  21. progressLine.progress = progress;
  22. }
  23.  
  24.  
  25. - (void)connectionDidFinishLoading:(NSURLConnection*)theConnection {
  26.  
  27. [connection release];
  28. connection=nil;
  29.  
  30. UIImageView* imageView = [[[UIImageView alloc] initWithImage:[UIImage imageWithData:data]] autorelease];
  31. imageView.frame = self.view.bounds;
  32. imageView.contentMode = UIViewContentModeScaleAspectFit;
  33. imageView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth || UIViewAutoresizingFlexibleHeight );
  34. [self.view addSubview:imageView];
  35.  
  36. [data release];
  37. data=nil;
  38. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.