Question about NSURLConnection and cache(ing)
Question about NSURLConnection and cache(ing)
- Subject: Question about NSURLConnection and cache(ing)
- From: "Eric E. Dolecki" <email@hidden>
- Date: Fri, 15 May 2009 11:02:10 -0400
I just started playing with NSURLConnection - and in my example everything
seems to work on initial load of an image asset. Once it's cached, I'd think
I'd get the cached version of the URL back instead, and I don't see anything
in my UIImageView... and the size is reported incorrectly.
When it runs correctly, I get the string "Received image containing 2546227
bytes of data" - when it's run and I get no image it states 19534 bytes of
data.
Below is my simple code - what am I doing wrong here?
I define receivedData as NSMutableData in my .h
- (void)connection:(NSURLConnection *)connection
didReceiveResponse:(NSURLResponse *)response {
NSLog(@"Can proceed");
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData
*)data {
NSLog(@"got data");
[receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError
*)error {
[connection release];
[receivedData release];
// inform the user
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
//NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
int r = [receivedData length];
int k = r/1024;
NSString *line = [NSString stringWithFormat:@"Received image containing %d
bytes of data", [receivedData length]];
label.text = line;
label2.text = [NSString stringWithFormat:@"That translates to %dk", k];
UIImage *retData = [[UIImage alloc] initWithData:receivedData];
imageView.image = retData;
[spinner stopAnimating];
// release the connection, and the data object
[connection release];
[receivedData release];
[retData release];
}
// Implement viewDidLoad to do additional setup after loading the view,
typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
spinner.hidesWhenStopped = YES;
[spinner startAnimating];
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL
URLWithString:@"
http://interfacelift.com/wallpaper_beta/Daf8b5cc/01902_stpetersdome_1680x1050.jpg
"]
cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSURLConnection *theConnection = [[NSURLConnection alloc]
initWithRequest:theRequest delegate:self];
if (theConnection) {
receivedData=[[NSMutableData data] retain];
} else {
// Let user know
}
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden