2

我目前正在为 OS X 开发一个应用程序,向后兼容 OS X 10.6。在某些时候,我创建了一个 WebView,我在其中加载了我动态创建的 html 内容。html 内容仅由图像链接<img src=和文本组成,没有 javascript 或类似的东西。所有图像(只有 5 张 png 图像)都存储在本地,大小为 4 KB。

我遇到的问题是某些图像(那些不在“滚动”可见侧的图像),在我第一次运行应用程序时,图像不会显示,除非我将窗口拖到另一个屏幕或再次加载包含 WebView 的视图控制器。在这些情况下,即使图像不在现场,图像也会出现在“滚动”上。

我已经尝试使用 IB 和以编程方式创建 WebView,我使用了 Autosaves、AllowsAnimatedImages 等 WebPreferences……我尝试使用 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];

我还应该提到,如果图像被捕获在“滚动”的可见侧和不可见侧之间的某个位置,即使页面向上滚动,也只会呈现图像的可见位......所以我认为所有这是一些渲染问题...

感谢您的帮助,谢谢!

4

1 回答 1

0

好的,我发现问题出在哪里。事情是 webview 内容正在 webview 框架内呈现,但框架比文档小。所以我通过将以下代码添加到 webview:didFinishLoadForFrame 解决了这个问题:

- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{  
     //Get the rect for the rendered frame
     NSRect webFrameRect = [[[[miwebView mainFrame] frameView] documentView] frame];
     //Get the rect of the current webview
     NSRect webViewRect = [miwebView frame];
     //Calculate the new frame
     NSRect newWebViewRect = NSMakeRect(webViewRect.origin.x,
                                           webViewRect.origin.y - (NSHeight(webFrameRect) - NSHeight(webViewRect)),
                                           NSWidth(webViewRect),
                                           NSHeight(webFrameRect));

    [miwebView setFrame:newWebViewRect];
    [miwebView setFrame:webViewRect];
    [[[miwebView mainFrame] frameView] setAllowsScrolling:YES];
}
于 2013-11-05T08:49:33.727 回答