【问题标题】:Replacing image source with UIImage in UIWebiew在 UIWebiew 中用 UIImage 替换图像源
【发布时间】:2013-03-06 10:45:26
【问题描述】:

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

【问题讨论】:

    标签: javascript html ios css uiwebview


    【解决方案1】:

    我终于用下面的代码解决了这个问题。这可能效率低下,但这是我目前所能得到的。

    __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]]];
    

    【讨论】:

      猜你喜欢
      • 2014-02-10
      • 1970-01-01
      • 2011-07-11
      • 2012-06-23
      • 1970-01-01
      • 1970-01-01
      • 2011-09-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多