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