【问题标题】:UIWebView is blank and it's not loading an image from ResourcesUIWebView 是空白的,它没有从资源加载图像
【发布时间】:2012-02-05 10:51:15
【问题描述】:

我正在使用此代码在 UIWebView 中显示图像:

-(void)showImageWithLoadRequest{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"accent" ofType:@"png"];
    NSURL *imageUrl = [NSURL fileURLWithPath:path];
    NSURLRequest *imageRequest = [NSURLRequest requestWithURL:imageUrl];
    web.scalesPageToFit = YES;
    [web loadRequest:imageRequest];
}

问题是当我在模拟器中打开应用程序时,它会显示一个空白的 UIWebView,没有任何图像。

有什么办法可以解决这个问题吗?
我需要从我的资源中加载图像。在我的资源中,我有 2 个图像本地化。可能是问题?

编辑

正如 Seamus 建议的那样,现在我正在使用:

NSString *path = [[NSBundle mainBundle] pathForResource:@"accent" ofType:@"png"];
[myWebView loadHTMLString:[NSString stringWithFormat:@"<html><body><img src=\"file://%@\"></body></html>",path] baseURL:nil];

但它仍然显示一个空白的 WebView。有什么想法吗?

【问题讨论】:

    标签: objective-c image uiwebview


    【解决方案1】:

    我这样做是使用 html。 uiwebviewloadHTMLString:baseURL:

    你可以像这样设置htmlSourceCode:

    htmlSourceCode = [[NSString alloc] initWithString:@"<html><body><img scr='file://%@'></body></html>",[img path]];
    [webview loadHTMLString:htmlSourceCode baseURL:nil];
    

    【讨论】:

    • 所以,我使用的是代码:NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"png"]; [myWebView loadHTMLString:[NSString stringWithFormat:@"&lt;html&gt;&lt;body&gt;&lt;img src=\"file://%@\"&gt;&lt;/body&gt;&lt;/html&gt;",path] baseURL:nil]; 但它仍然显示一个空白窗口!它没有加载图像。有什么想法吗?
    • @Pheel 检查accent.png 是否存在。如果您不使用 ib,请检查 webview.frame。
    猜你喜欢
    • 2013-08-26
    • 2012-11-15
    • 1970-01-01
    • 1970-01-01
    • 2011-08-09
    • 2011-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多