我正在编写一个解析嵌入了图像和数据的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]]];
答案 0 :(得分:2)
我终于用下面的代码解决了这个问题。这可能是低效的,但这是我目前所能得到的。
__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]]];