【问题标题】:UIWebView to UIImage invalid context 0x0UIWebView 到 UIImage 无效上下文 0x0
【发布时间】:2012-06-10 19:21:33
【问题描述】:

我正在尝试做一个简单的任务;将 UIWebView 的内容转换为 UIImage 并将其保存到手机的文档目录中,但是每次运行以下代码时,我都会收到一堆类似的错误:

  UIGraphicsBeginImageContext(rect.size);
    [self.webView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

产生以下错误:

Jun 10 20:06:18 <Error>: CGContextSaveGState: invalid context 0x0
Jun 10 20:06:18 <Error>: CGContextSetAlpha: invalid context 0x0
Jun 10 20:06:18 <Error>: CGContextSaveGState: invalid context 0x0
Jun 10 20:06:18 <Error>: CGContextSetFillColorWithColor: invalid context 0x0
Jun 10 20:06:18 <Error>: CGContextAddRect: invalid context 0x0
Jun 10 20:06:18 <Error>: CGContextDrawPath: invalid context 0x0

我做了一些研究,发现这是由于上面的代码不在 -(void)drawRect:(CGRect)rect 方法中,但是,在 UIWebView 初始化后如何调用它,或者我应该不手动调用?

这是我用来创建 HTML 并将其加载到 WebView 中的代码:

-(void)saveHTMLDocument {

    NSMutableString *htmlDocumentToSave = [[NSMutableString alloc] initWithString:@"<html><body>"];
    [htmlDocumentToSave appendString:@"<table border=""1""><tr><th>Snag Number</th><th>Snag Photo</th><th>Snag Description</th><th>Date Taken</th>"];

    for (int i = 0; i < self.fetchedResultsController.fetchedObjects.count; i++) {

    NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation([self.photoArray objectAtIndex:i])];
    NSString *base64String = [imageData base64EncodedString];

    Snag *snag = [self.fetchedResultsController.fetchedObjects objectAtIndex:i];

    NSString *tableString = [NSString stringWithFormat:@"<tr><td>%i</td><td><p><b><img src='data:image/png;base64,%@'></b></p></td><td>%@</td><td>%@</td></tr>", i+1, base64String, snag.snagDescription, snag.dateTaken];
    [htmlDocumentToSave appendString:tableString];

    }

    [htmlDocumentToSave appendString:@"</table></body></html>"];


    //Save the HTMl document that will be attached.
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];

    NSString *htmlPath = [documentsPath stringByAppendingPathComponent:@"test"];
    [htmlDocumentToSave writeToFile:htmlPath atomically:NO encoding:NSUTF8StringEncoding error:nil];
    self.htmlData = [NSData dataWithContentsOfFile:htmlPath];

    //Generate the UIWebView and load the HTML into it

    NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"html"];

    NSData *htmlData = [NSData dataWithContentsOfFile:htmlFile];

    self.webView = [[UIWebView alloc] init];
    [self.webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@""]];

    NSString *string = [self.webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('html')[0].innerHTML"];

    NSLog(@"Check if WebView has loaded %@", string);



}

-(void)drawRect:(CGRect)rect {

    UIGraphicsBeginImageContext(rect.size);
    [self.webView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}

【问题讨论】:

    标签: objective-c ios uiwebview drawrect


    【解决方案1】:

    不,它不必在drawRect中,我使用这个函数

    +(UIImage*)captureScreen:(UIView*) viewToCapture
    {
        UIGraphicsBeginImageContextWithOptions(viewToCapture.bounds.size, viewToCapture.opaque, 0.0);
        [viewToCapture.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return viewImage;
    }
    

    在我的应用中,它正确地截取了我的 WebView 的屏幕截图

    【讨论】:

    • 当与 UIWebViews 一起使用时,这种技术在旧版本的 iOS 中是不稳定的,尽管我最近没有尝试过。另一个潜在问题是试图弄清楚 UIWebView 何时完成显示内容。
    • 是的,确实,我倾向于在 webview 完成加载后将其延迟 300 - 500 毫秒,我在 iOS 4.3 和 5.1 中使用它并且效果很好,我目前使用它来动画页面过渡一个 ePub 查看器,效果很好
    • 谢谢 Omar,一个简单的问题,我该如何调用该方法?
    • 我这样称呼它 pageView.pageImage = [Utilities captureScreen:webView];其中 pageImage 是 UIImage Utilities 是一个实用程序类,我只向它添加类方法另外,如果有帮助,请考虑投票和/或接受答案:)
    • @OmarAbdelhafith 我仍然收到 CGContextSaveGState: invalid context 0x0 using your method。
    【解决方案2】:

    rect.size 是什么?如果宽度或高度为零,则 CG 将无法创建上下文,从而导致您看到的各种错误。

    @OmarAbdelhafith 关于其余部分是正确的——您不必在 -drawRect: 中执行此工作,并且您的其余代码看起来还可以。另外,请确保您在 UIWebView 完全加载后运行此代码。

    【讨论】:

    • 某些控件的大小为宽度:0 高度:0。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 2013-10-30
    • 2012-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多