我正在嘗試做一個簡單的任務;一個UIWebView的內容轉換到一個UIImage並保存到手機的文件目錄,但是我一直在每一個下面的代碼運行時得到了一堆類似的錯誤:UIWebView到UIImage無效的上下文0x0
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並將其加載到web視圖代碼:
-(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();
}
與UIWebViews一起使用時,這種技術在舊版本的iOS中很片面,儘管我最近沒有嘗試過。另一個潛在的問題是試圖找出UIWebView何時完成顯示內容。 – EricS
確實是這樣,我傾向於從300延遲它 - web視圖加載完成後500毫秒,我在的iOS 4.3和5.1使用這個和它的作品很好,我目前使用它ePub的觀衆動畫頁面轉換,它工作得不錯 –
感謝奧馬爾,一個簡單的問題,我該如何稱呼這種方法? – jcrowson