我有一个带有嵌入式 UIWebview (Safari) 控件的 iPhone 应用程序。我希望能够在本地存储来自某些网页的图像。
我可以以编程方式访问 UIWebview 下载图像的路径吗?还是我需要先解析 HTML 再去网上找图片资源再下载?
我不知道您是否可以从 Safari 的缓存中访问预加载的图像...
但是,您可以通过运行一些 javascript 来轻松找到图像 URL,而无需解析 HTML:
NSString *script = @"var n = document.images.length; var names = [];"
"for (var i = 0; i < n; i++) {"
" names.push(document.images[i].src);"
"} String(names);";
NSString *urls = [webView stringByEvaluatingJavaScriptFromString:script];
// urls contains a comma-separated list of the image URLs.
disclaimer: I have not tried this.
If you can find out their names, UIImage responds to +(UIImage*)imageNamed:(NSString*) which the docs imply might get the image back from a device wide cache.
I'd be interested in knowing the results of any experiments.
我不认为你可以直接使用 UIWebview 保存图像,你将不得不手动获取图像......