I need to cache an image with a web source (i.e. not associated with the source) that is included in the web view. Usually I just request an image and convert it to base-64 and paste it into HTML, but if I have 100 images per line, I cannot do this.
So, I searched a bit and found a way to capture an image request by subclassing NSURLCache and overriding cachedResponseForRequest . My code is below.
I can make the request just fine, I can return the data and print it, I made a UIImage from the data and got its size, to know that it definitely is, etc. But when I return cachedResponseForRequest and look at the UIWebView , it just shows the missing image. I was tormented this afternoon and I canβt understand. I looked at the example after the example and the cigar.
Can someone help me solve this problem? It drives me crazy...
Here is the contents of the .m file of my subclass NSURLCache . GimageData is an instance variable for storing the current received image data.
Just so you know, the reason I am searching / replacing http___ with http: // is because the http: // links don't seem to get this method, so I made the link look like it then In the package, I return the URL to retrieve.
- (NSCachedURLResponse*)cachedResponseForRequest:(NSURLRequest*)request { NSURL *url = [request URL]; NSLog(@"Url requested is: %@", [url absoluteString] ); NSRange textRange; textRange = [[[url absoluteString] lowercaseString] rangeOfString:[@"http___" lowercaseString]]; NSCachedURLResponse *cachedResponse = nil; if(textRange.location != NSNotFound) { //Does contain the substring NSLog(@"Found an http___ request"); NSString *newURL = [[url absoluteString] substringFromIndex:textRange.location]; newURL = [newURL stringByReplacingOccurrencesOfString:@"http___" withString:@"http://"]; NSLog(@"New url: %@", newURL); TTURLDataResponse *response = [[TTURLDataResponse alloc] init]; TTURLRequest *imageRequest = [[TTURLRequest alloc] initWithURL:newURL delegate:self]; imageRequest.response = response; [response release]; [imageRequest sendSynchronously]; if ( GimageData ) { NSLog(@"Response: %d as %@", [GimageData bytes], [GimageData class]); NSLog(@"Storing under: %@ ", request.URL ); NSURLResponse* Nresponse = [[NSURLResponse alloc] initWithURL:request.URL MIMEType:nil expectedContentLength:[GimageData length] textEncodingName:nil]; cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:Nresponse data:GimageData]; [imageRequest release]; [Nresponse release]; NSLog(@"Cached the response."); } } if (cachedResponse == nil) { NSLog(@"Response is nil so getting super cache"); cachedResponse = [super cachedResponseForRequest:request]; } return cachedResponse; } - (void)requestDidFinishLoad:(TTURLRequest*)request { NSLog(@"Request did finish loading."); TTURLDataResponse *response = request.response; // NSURL *url = [NSURL URLWithString:request.urlPath]; GimageData = response.data; if ( GimageData ) { NSLog(@"And we got the data."); } else { NSLog(@"But there was no data in the response."); } }
source share