2012-11-14 80 views
2

我想要做的是在每次更改/加載webview的內容時將UIWebView轉換爲UIImage。如何正確地將UIWebView與Chaning內容轉換爲UIImages

下面是代碼:

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

我不知道爲什麼,但一些創建UIImages是重複的。這可能是因爲線程,並沒有完成轉換爲UIImage,但延遲開始加載webview的新內容(即使我將它設置爲5secs)不會改變任何東西。

有人可以解釋我爲什麼得到重複UIImages?

回答

0

webViewDidFinishLoad:WebView完成加載幀後調用。所以你得到重複的那些頁面可能包含多個框架。

+0

謝謝回覆。爲什麼他們有多個框架,用於測試我設置webview的內容相同的簡單html? – sag

+0

在這種情況下,我不知道你的問題的原因。 – lammert

1

我得到它的工作... 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]; 
    } 
} 
相關問題