应用程序文档文件夹中的UIWebView图像无法刷新

时间:2022-11-08 00:25:43

I have a UIWebView where I display dynamically generated HTML via

我有一个UIWebView,我通过它显示动态生成的HTML

[_webView loadHTMLString:htmlString baseURL:applicationDocumentsDirectory];

The HTML contains images such as <img src="chart1.png">. Each time the HTML is generated, the images are also freshly generated. (I have checked that they are indeed in the right location and updated.) However, after the first run, if I change the data and relaunch my UIWebView, it uses the old images.

HTML包含应用程序文档文件夹中的UIWebView图像无法刷新等图像。每次生成HTML时,图像也都是新生成的。 (我已经检查过它们确实位于正确的位置并进行了更新。)但是,在第一次运行后,如果我更改数据并重新启动我的UIWebView,它将使用旧图像。

I have tried:

我试过了:

[[NSURLCache sharedURLCache] removeAllCachedResponses];

to no avail. (Not surprising, maybe, as this relates to NSURLRequests.)

无济于事。 (也许并不奇怪,因为这与NSURLRequests相关。)

I know that there is the possibility of absolute URLs, but I have not tried this at it seems cumbersome and I want it to work with these simple relative URLs. The issue is not finding the images but updating them appropriately.

我知道有绝对URL的可能性,但我没有尝试过这看起来很麻烦,我希望它能够使用这些简单的相对URL。问题不是找到图像,而是适当地更新它们。

I also know that you could invent some scheme tricking the browser into thinking that the image is dynamic by changing the src to something like chart1.png?1234 where the number is random generated and always unique. This also seems like a useless workaround for an issue that should be simple to solve.

我也知道你可以通过将src更改为像chart1.png?1234这样的数字随机生成并且始终是唯一的,可以发明一些欺骗浏览器认为图像是动态的方案。对于一个应该易于解决的问题,这似乎也是一种无用的解决方法。

Any ideas?

3 个解决方案

#1


0  

You should have a proper reference to the image, otherwise it may not work. If you have the image and the html file in the same level then try using something like this,

您应该对图像有适当的引用,否则它可能不起作用。如果你有相同级别的图像和html文件,那么尝试使用这样的东西,

<img src="./chart1.png">

Or if you want the images inside the folder then refer it using the proper path reference to it.

或者,如果您想要文件夹中的图像,请使用正确的路径引用来引用它。

I hope it works.

我希望它有效。

#2


0  

hi i was having a similar problem with uiwebview whenever i loaded a previously loaded page old content was displayed. after investigating further i found that uiwebview content is loaded in the background and then displayed after its finished loading; however while it is loading old content is displayed. I fixed my problem by making my own uiwebview loading delegates and adding my own loading symbol insteaad of displaying old content.

嗨我在uiwebview遇到类似的问题,每当我加载一个以前加载的页面时,显示旧的内容。在进一步调查后,我发现uiwebview内容在后台加载,然后在完成加载后显示;但是在加载旧内容时会显示。我修复了我的问题,让我自己的uiwebview加载代理并添加我自己的加载符号insteaad显示旧内容。

    -(void)webViewDidStartLoad:(UIWebView *)webView{

webView.hidden = true;
loader.animationImages =[NSArray arrayWithObjects:

                            [UIImage imageNamed:@"1.png"],
                            [UIImage imageNamed:@"2.png"],
                            [UIImage imageNamed:@"3.png"],
                            [UIImage imageNamed:@"4.png"],
                            [UIImage imageNamed:@"3.png"],
                            [UIImage imageNamed:@"2.png"],
                            [UIImage imageNamed:@"1.png"],
                            [UIImage imageNamed:@"5.png"]
                             ,nil];


loader.animationDuration = 1.5;

loader.animationRepeatCount = 100;
[loader startAnimating];

}
-(void)webViewDidFinishLoad:(UIWebView *)webView{

[loader stopAnimating];
webView.hidden  = false;
}

remember to link the delegates and all that other stuff i hope this helps :)

记得链接代表和所有其他东西,我希望这有帮助:)

#3


-1  

You need to provide the absolute path of the image source.

您需要提供图像源的绝对路径。

NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                     NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:
                  @"/chart1.png" ];

I hope this works

我希望这有效

#1


0  

You should have a proper reference to the image, otherwise it may not work. If you have the image and the html file in the same level then try using something like this,

您应该对图像有适当的引用,否则它可能不起作用。如果你有相同级别的图像和html文件,那么尝试使用这样的东西,

<img src="./chart1.png">

Or if you want the images inside the folder then refer it using the proper path reference to it.

或者,如果您想要文件夹中的图像,请使用正确的路径引用来引用它。

I hope it works.

我希望它有效。

#2


0  

hi i was having a similar problem with uiwebview whenever i loaded a previously loaded page old content was displayed. after investigating further i found that uiwebview content is loaded in the background and then displayed after its finished loading; however while it is loading old content is displayed. I fixed my problem by making my own uiwebview loading delegates and adding my own loading symbol insteaad of displaying old content.

嗨我在uiwebview遇到类似的问题,每当我加载一个以前加载的页面时,显示旧的内容。在进一步调查后,我发现uiwebview内容在后台加载,然后在完成加载后显示;但是在加载旧内容时会显示。我修复了我的问题,让我自己的uiwebview加载代理并添加我自己的加载符号insteaad显示旧内容。

    -(void)webViewDidStartLoad:(UIWebView *)webView{

webView.hidden = true;
loader.animationImages =[NSArray arrayWithObjects:

                            [UIImage imageNamed:@"1.png"],
                            [UIImage imageNamed:@"2.png"],
                            [UIImage imageNamed:@"3.png"],
                            [UIImage imageNamed:@"4.png"],
                            [UIImage imageNamed:@"3.png"],
                            [UIImage imageNamed:@"2.png"],
                            [UIImage imageNamed:@"1.png"],
                            [UIImage imageNamed:@"5.png"]
                             ,nil];


loader.animationDuration = 1.5;

loader.animationRepeatCount = 100;
[loader startAnimating];

}
-(void)webViewDidFinishLoad:(UIWebView *)webView{

[loader stopAnimating];
webView.hidden  = false;
}

remember to link the delegates and all that other stuff i hope this helps :)

记得链接代表和所有其他东西,我希望这有帮助:)

#3


-1  

You need to provide the absolute path of the image source.

您需要提供图像源的绝对路径。

NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                     NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:
                  @"/chart1.png" ];

I hope this works

我希望这有效