gpt4 book ai didi

javascript - 在 UIWebiew 中用 UIImage 替换图像源

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:15:28 25 4
gpt4 key购买 nike

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

最佳答案

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

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

关于javascript - 在 UIWebiew 中用 UIImage 替换图像源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15245171/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com