[英]Get loaded images from UIWebView
在我的应用程序中有一个UIWebView
。 其中加载的网页有一些图像,我想在其他地方使用这些图像(例如在UIImageView
显示它们)
那么是否可以直接从UIWebView
获取加载的图像而无需再次下载它们? 我现在正在做的是从html文件获取图像的URL并下载它们,但这太费时间了。
我已经想到了这一点:关键是从NSURLCache
提取图像。 但是,从iOS 8开始,您似乎需要将默认缓存设置为application:didFinishLaunchingWithOptions:
的第一件事application:didFinishLaunchingWithOptions:
为此工作。 例如:
在application:didFinishLaunchingWithOptions:
[NSURLCache setSharedURLCache:[[NSURLCache alloc]
initWithMemoryCapacity:32*1024*1024 diskCapacity:64*1024*1024 diskPath:...]
然后在UIWebView
完成加载后:
NSCachedURLResponse * response = [[NSURLCache sharedURLCache]
cachedResponseForRequest:[NSURLRequest requestWithURL:
[NSURL URLWithString:@"http://.../image.png"]]];
if (response.data)
{
UIImage * nativeImage = [UIImage imageWithData:response.data];
....
}
如果您还没有它,可以从UIWebView
获取一系列图像
NSArray * images = [[webView stringByEvaluatingJavaScriptFromString:
@"var imgs = []; for (var i = 0; i < document.images.length; i++) "
"imgs.push(document.images[i].src); imgs.toString();"]
componentsSeparatedByString:@","];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.