2

我正在编写一个应用程序来解析嵌入图像和数据的 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]]];
4

1 回答 1

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]]];
于 2013-03-07T08:28:58.953 回答