作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在编写一个应用程序来解析嵌入了图像和数据的 HTML 字符串。我能够使用 SDWebImage 来缓存图像部分以备后用,并且缓存非常完美。但是,我需要使用缓存图像 (UIImage) 来替代 html 字符串中的 src。我认为这可以通过一些 javascript 代码来完成,但是当涉及到 javascript 和 css 时,我完全是个笨蛋。有人可以给我一些 javascript 代码来将图像 src 更改为这个 UIImage 吗?
缓存图片的代码:
-(NSString *)fetchImageUrlFromSummary:(NSString *)summaryData
{
NSError *error = nil;
NSString *urlString = [NSString string];
HTMLParser *parser = [[HTMLParser alloc]initWithString:summaryData error:&error];
if (error) {
NSLog(@"%@", error);
return nil;
}
HTMLNode *bodyNode = [parser body];
NSArray *inputNodes = [bodyNode findChildTags:@"img"];
for (HTMLNode *inputNode in inputNodes) {
//NSLog(@"%@", [inputNode getAttributeNamed:@"src"]);
if ([[inputNode getAttributeNamed:@"src"]hasSuffix:@"png"] ||
[[inputNode getAttributeNamed:@"src"]hasSuffix:@"jpg"]||
[[inputNode getAttributeNamed:@"src"]hasSuffix:@"jpeg"]) {
urlString = [inputNode getAttributeNamed:@"src"];
[SDWebImagePrefetcher.sharedImagePrefetcher prefetchURLs:[NSArray arrayWithObject:urlString]];
}
}
return urlString;
}
在uiwebview中设置摘要字符串的代码:
NSString *summaryString = [[NSString alloc]initWithData:[_htmlData summary] encoding:NSUTF8StringEncoding];
NSString *myDescriptionHTML = [NSString stringWithFormat:@"<html><head><style type=\"text/css\"> div{text-align:justify;text-justify:inter-word;margin:auto}
img {max-width:100%%;width:auto;height:auto;}body{font-family: Verdana;font-size: 12pt;max-width: 100%%; width:auto; height:auto;}
<body bgcolor=\"#F5F5DC\"></style><body>%@</body></html>", summaryString];
[_webView loadHTMLString:myDescriptionHTML baseURL:[NSURL URLWithString:[_htmlData originUrl]]];
最佳答案
我终于用下面的代码解决了这个问题。这可能效率低下,但这就是我目前所能得到的。
__block NSData *imageData = [NSData data];
SDImageCache *imageCache = [SDImageCache sharedImageCache];
[imageCache queryDiskCacheForKey:_articles.articleImageURL done:^(UIImage *image, SDImageCacheType cacheType)
{
imageData = UIImagePNGRepresentation(image);
NSMutableString *summaryString = [[NSMutableString alloc]initWithData:[_articles articleSummary] encoding:NSUTF8StringEncoding];
NSString *headerString = [NSString stringWithFormat:@"<html><head><style type=\"text/css\"> div{text-align:justify;text-justify:inter-word;margin:auto}img {max-width:100%%;width:auto;height:auto;}body{font-family: Verdana;font-size: 12pt;max-width: 100%%; width:auto; height:auto;}<body bgcolor=\"#F5F5DC\"></style><body>"];
if (image) {
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<img[^>]*>"
options:NSRegularExpressionCaseInsensitive
error:nil];
[regex replaceMatchesInString:summaryString
options:0
range:NSMakeRange(0, summaryString.length)
withTemplate:@""];
NSString *imageSource = [NSString stringWithFormat:@"data:image/png;base64,%@",[imageData base64Encoding]];
NSString *imageSOruceWithTag = [NSString stringWithFormat:@"<img src='%@' />", imageSource];
NSMutableString *myDescriptionHTML = [NSMutableString stringWithFormat:@"%@%@%@</body></html>",headerString, imageSOruceWithTag,summaryString];
[_webView loadHTMLString:myDescriptionHTML baseURL:[NSURL URLWithString:[_articles originUrl]]];
}
else {
NSString *descriptionHTML = [NSString stringWithFormat:@"%@%@</body></html>",headerString,summaryString];
[_webView loadHTMLString:descriptionHTML baseURL:[NSURL URLWithString:[_articles originUrl]]];
关于javascript - 在 UIWebiew 中用 UIImage 替换图像源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15245171/
我正在编写一个应用程序来解析嵌入了图像和数据的 HTML 字符串。我能够使用 SDWebImage 来缓存图像部分以备后用,并且缓存非常完美。但是,我需要使用缓存图像 (UIImage) 来替代 ht
我已将 WebView 放置在 Scrollview 中,而 Scrollview 又放置在 viewcontroller 的 View 中。在 webview 上点击时,不会调用“tapRecogn
我是一名优秀的程序员,十分优秀!