PDA

View Full Version : reloading image from web




pamelaraj
Apr 22, 2009, 03:04 PM
#################################

-(IBAction)touchDownLoad:(id)sender
{
NSMutableString *url = [[NSMutableString alloc]init];
[url appendString:@"http://www.beyond2000.co.za/images/2006-4/Large/smal_janene_large.jpg"];

theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]
cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];
[url release];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if( theConnection)
{
receivedData = [[NSMutableData data] retain];
lblMessage.text = @"receivedData";
}

}

-(IBAction)cancelView:(id)sender
{
[imageDownload autorelease];
[imageDownload removeFromSuperview];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
lblMessage.text = @"connection didReceiveResponse";
[receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
lblMessage.text = @"connection didReceiveData";
[receivedData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
lblMessage.text = [NSString stringWithFormat:@"connectionDidFinishLoading: %@", receivedData];
// doing something with receivedData
imageDownload.image = [UIImage imageWithData:receivedData];
}

##############################

When I run the program, it downloads the image from the specified Url. When I click the "Cancel" button (which implements the "CancelView" method ), it removes the image. But when I click the "download" (which calls the "touchDownLoad" method) button, it just exists out. I want the download and the cancel button to work numerous times and not just once. How could I do that?

Thanks.



pamelaraj
Apr 22, 2009, 03:53 PM
instead of this code:
######
-(IBAction)cancelViewid)sender
{
[imageDownload autorelease];
[imageDownload removeFromSuperview];
}
######

I used
#######

-(IBAction)cancelView:(id)sender
{
imageDownload.image = nil;
}
#######