【发布时间】:2013-10-22 13:12:49
【问题描述】:
我目前正在为 OS X 开发一个应用程序,向后兼容 OS X 10.6。在某些时候,我创建了一个 WebView,我在其中加载了我动态创建的 html 内容。 html 内容仅由图像链接<img src= 和文本组成,没有 javascript 或类似的东西。所有图像(只有 5 张 png 图像)都存储在本地,大小为 4 KB。
我遇到的问题是某些图像(那些不在“滚动”可见侧的图像),在我第一次运行应用程序时,除非我将窗口拖到另一个屏幕或再次加载包含 WebView 的视图控制器。在这些情况下,即使图像不在现场,图像也会出现在“滚动”上。
我尝试使用 IB 和编程方式创建 WebView,我使用了 WebPreferences,如 Autosaves、AllowsAnimatedImages……我尝试使用 NSURLCache 加载每个图像,以便 WebView 更容易访问它们……同样结果。
考虑到我的代码非常广泛,我将只发布我认为相关的部分:
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];
[self.miwebView setResourceLoadDelegate:self];
[self.miwebView setFrameLoadDelegate:self];
WebPreferences *webPref = [[WebPreferences alloc]init];
[webPref setAutosaves:YES];
[webPref setAllowsAnimatedImages:YES];
[webPref setAllowsAnimatedImageLooping:YES];
[self.miwebView setPreferences:webPref];
...
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];
...
NSString *pathResult = [[NSBundle mainBundle] bundlePath];
NSURL *baseURLRes = [NSURL fileURLWithPath:pathResult];
[[self.miwebView mainFrame] loadHTMLString:finalHtml baseURL:baseURLRes];
[self.fixedView addSubview:self.miwebView];
我还应该提到,如果图像被捕获在“滚动”的可见侧和不可见侧之间的某个位置,即使页面向上滚动,也只会呈现图像的可见位......所以我认为所有这些都是一些渲染问题...
感谢您的帮助,谢谢!
【问题讨论】:
-
我必须补充一点,这个问题在 OS X 10.7 中不存在,它只在 10.8 中发生
标签: html image macos cocoa webview