簡単な作業をしようとしています。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();
}