Return to Snippet

Revision: 43585
at March 27, 2011 04:07 by off


Initial Code
- (IBAction)download:(id)sender
{
    NSURL *url = [NSURL URLWithString:@"http://123.jpg"];
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10.0];
    connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    connection = nil;
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    self.filesize = [NSNumber numberWithUnsignedInteger:[response expectedContentLength]];

}

- (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)recievedData {
    if (data==nil) {
		data =	[[NSMutableData alloc] initWithCapacity:2048];
    }
    [data appendData:recievedData];
    NSNumber *resourceLength = [NSNumber numberWithUnsignedInteger:[data length]]; //MAGIC
    float progress = [resourceLength floatValue] / [self.filesize floatValue];
    progressLine.progress = progress;
}


- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection {
	
    [connection release];
    connection=nil;
	
    UIImageView* imageView = [[[UIImageView alloc] initWithImage:[UIImage imageWithData:data]] autorelease];
    imageView.frame = self.view.bounds;	
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth || UIViewAutoresizingFlexibleHeight );
    [self.view addSubview:imageView];

    [data release];
    data=nil;
}

Initial URL


Initial Description


Initial Title
download file with UIProgressView

Initial Tags
image, file, download, iphone

Initial Language
Objective C