[英]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.