gpt4 book ai didi

iphone - 如何正确转换 UIWebView 将内容更改为图像

转载 作者:行者123 更新时间:2023-12-03 21:15:59 24 4
gpt4 key购买 nike

我想做的是每次更改/加载 webview 的内容时将 UIWebView 转换为 UIImage 。

这是代码:

- (void)viewDidLoad {
[self.tmpWebView loadHTMLString:[NSString stringWithFormat:@"Some %i HTML", 0,baseURL:nil];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {

if (webView==tmpWebView && pngCount<[self.arrayOfHtmlContent count]) {
UIGraphicsBeginImageContext(webView.frame.size);
{
[webView.layer renderInContext: UIGraphicsGetCurrentContext()];
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(10, 10+((pngCount==1)?0:(pngCount-1)*100), 400, 200);
[imageView setBackgroundColor:[UIColor redColor]];
[_someScrollView addSubview:imageView];
//NSString *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat: @"Documents/Test-%i.png",pngCount]];
//[UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES];
}
UIGraphicsEndImageContext();

[self performSelector:@selector(loadWebViewWith:) withObject:[[NSArray alloc] initWithObjects:webView, [[NSNumber alloc] initWithInt:pngCount], nil ] afterDelay:0.3];//DELAY INCREASING DOES NOT CHANGE ANYTHING

pngCount++;
//NSLog(@"Finish%i: %@",(pngCount-1),[webView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"]);
}
}

- (void) loadWebViewWith:(NSArray*)array {
[(UIWebView*)[array objectAtIndex:0] loadHTMLString:[NSString stringWithFormat:@"Another %i HTML", [[array objectAtIndex:1] intValue]] baseURL:nil];
}

我不知道为什么,但是一些创建的 UIImage 是重复的。也许是因为 Thread,它没有完成转换为 UIImage,但是开始加载 webview 新内容的延迟(即使我将其设置为 5 秒)不会改变任何内容。

有人可以解释一下为什么我会得到重复的 UIImages 吗?

最佳答案

我让它工作了... WebView 必须添加到 ViewController.view 中。显然原因是主线程的优先级......

所以,这是代码:

- (void)viewDidLoad {

pngCount = 0;

[self.tmpWebView loadHTMLString:[NSString stringWithFormat:@"Some %i HTML", pngCount] baseURL:nil];

[self.view insertSubview:self.tmpWebView atIndex:0];/////////
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
if (webView==self.tmpWebView && pngCount<[self.arrayOfHtmlContent count]) {

[self performSelector:@selector(webViewToImage) withObject:nil afterDelay:0.05];

[self performSelector:@selector(loadWebViewContent) withObject:nil afterDelay:0.1];
}
}


- (void) webViewToImage {

UIGraphicsBeginImageContext(self.tmpWebView.frame.size);
{
[self.tmpWebView.layer renderInContext: UIGraphicsGetCurrentContext()];
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();

UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(10, 110+((pngCount==1)?0:(pngCount-1)*100), 200, 90);
[imageView setBackgroundColor:[UIColor clearColor]];

UIControl *mask = [[UIControl alloc] initWithFrame:imageView.frame];

imageView.frame = CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height);
[mask addSubview:imageView];

[mask addTarget:self action:@selector(showOnMainWebView:) forControlEvents:UIControlEventTouchUpInside];
mask.tag = pngCount;

[_someScrollView addSubview:mask];
}
UIGraphicsEndImageContext();

pngCount++;
}

- (void) loadWebViewContent {

if (pngCount < [self.arrayOfHtmlContent count]) {
[self.tmpWebView loadHTMLString:[NSString stringWithFormat:@"Another %i HTML", pngCount] baseURL:nil];
}
}

关于iphone - 如何正确转换 UIWebView 将内容更改为图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13375093/

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