Cocoa WebView won't render all images on OSX 10.8
- by user2906962
I'm currently developing an application for OS X, backwards compatible with OS X 10.6. At some point I create a WebView in which I load html content that I create dynamically. The html content is formed only of image links <img src= and text, there is no javascript or anything of that kind. All the images (there are only 5 png images) are stored locally and their size is 4 KB.
The problem I have is that some images (those that are not on the visible side of the "scroll"), the very first time I run the application,the images are not shown unless I drag the window to another screen or load again the view controller that contains the WebView. In those cases the images appear on the "scroll" even if they are offsite.
I've tried creating the WebView both with IB and programatically, I've used WebPreferences like Autosaves, AllowsAnimatedImages … I've tried using NSURLCache to load each image so that the WebView will get access to them easier ... same result.
Taking into account that my code is quite extensive I'm gonna post only the bits that I think are relevant:
NSString *finalHtml ... //contains the complete html
CGRect screenRect = [self.fixedView bounds];
CGRect webFrame = CGRectMake(0.0f, 0.0f, screenRect.size.width, screenRect.size.height);
self.miwebView=[[WebView alloc] initWithFrame:webFrame];
[self.miwebView setEditable:NO];
[self.miwebView setUIDelegate:self];
...
NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024
diskCapacity:20 * 1024 * 1024
diskPath:nil];
[NSURLCache setSharedURLCache:URLCache];
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"line" ofType:@"png"];
NSURL *resourceUrl = [NSURL URLWithString:imagePath];
NSURLRequest *request = [NSURLRequest requestWithURL:resourceUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0f];
[URLCache cachedResponseForRequest:request];
...
[self.miwebView setResourceLoadDelegate:self];
WebPreferences *webPref = [[WebPreferences alloc]init];
[webPref setAutosaves:YES];
[webPref setAllowsAnimatedImages:YES];
[webPref setAllowsAnimatedImageLooping:YES];
[self.miwebView setPreferences:webPref];
NSString *pathResult = [[NSBundle mainBundle] bundlePath];
NSURL *baseURLRes = [NSURL fileURLWithPath:pathResult];
[[self.miwebView mainFrame] loadHTMLString:finalHtml baseURL:baseURLRes];
[self.fixedView addSubview:self.miwebView];
I should also mention that if an image is caught somewhere in between the visible and non visible side of the "scroll" only the visible bit of the image is going to be rendered even if the page gets scrolled up ... so I think all this is some rendering issue ...
I appreciate your help, thank you!