【问题标题】:Cocoa WebView won't render all the images on OSX 10.8Cocoa WebView 不会在 OSX 10.8 上渲染所有图像
【发布时间】: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


【解决方案1】:

好的,我已经找到问题所在了。事情是 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-02-17
    • 2013-06-28
    • 1970-01-01
    • 1970-01-01
    • 2022-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-06
    相关资源
    最近更新 更多