Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am writing an application that parses a HTML string embedded with images and data. I am able to use SDWebImage to cache the images part for later use and it is caching perfectly. However, I need to use the cached image (UIImage) as a replacement for the src in the html string. I think this can be done with some javascript code but I am a total duffer when it comes to javascript and css. Can someone give me some javascript code to change the image src to this UIImage?

Code to cache image:

-(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;
}

Code to set the summary string in 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]]];
share|improve this question

1 Answer

up vote 1 down vote accepted

I finally fixed the issue with the below code. It might be inefficient but this was all I could get for the moment.

__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]]];
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.