2012-05-16 2 views
1

전체 UIWebView (화면 내용조차도 제외)을 PNG로 캡처하려고합니다.iOS - 이미지로 UIWebView 캡쳐

[self.webview.layer renderInContext:resizedContext]; /// crash 

가 어떻게이 renderInContext 프로세스를 중지하여 충돌을 방지 할 수 있습니다 : 다음 코드는, 그러나 그것은이 라인에 추락 씨족 일부 아이 패드 1에서 대부분의 장치에서 잘 작동?

May 16 12:33:41 unknown SpringBoard[29] <Warning>: Application 'AppName' exited abnormally  with signal 11: Segmentation fault: 11 
May 16 12:33:41 unknown DTMobileIS[652] <Warning>: _memoryNotification : { 
    OSMemoryNotificationLevel = 0; 
    timestamp = "2012-05-16 19:33:41 +0000"; 
} 
May 16 12:33:41 unknown DTMobileIS[652] <Warning>: _memoryNotification : <NSThread: 0x1d5286d0>{name = (null), num = 1} 
May 16 12:33:41 unknown DTMobileIS[652] <Warning>: _memoryNotification : { 
    OSMemoryNotificationLevel = 0; 
    timestamp = "2012-05-16 19:33:41 +0000"; 
} 
+0

충돌이 발생했을 때 예외는 무엇입니까? –

+0

이상한 예외는 없습니다. 그것은 응용 프로그램이 메모리 부족 예외에 도달하고 didReceiveMemoryWarning을 호출하는 것보다 단순히 로그가없는 경우와 충돌합니다. – James

+0

@ try/@ catch 블록에서 줄을 줄 바꿈하여 분석 할 예외가 생성되는지 확인하십시오. 도움이되지 않을 수도 있습니다. 상처받을 수 없다. (마지막으로 Xcode의 예외보고 기능에 대한 믿음이 3.x였습니다.) –

답변

1

I :

문제의 컨텍스트에 대한 코드 샘플 아래 참조 : 장치에서 실행할 때

//Create original tmp bounds 
CGRect tmpFrame = self.webview.frame; 
CGRect tmpBounds = self.webview.bounds; 
CGRect aFrame = self.webview.bounds; 
aFrame.size.width = self.webview.frame.size.width; 
aFrame.size.height = self.webview.frame.size.height; 
self.webview.frame = aFrame; 
aFrame.size.height = [self.webview sizeThatFits:[[UIScreen mainScreen] bounds].size].height; 

NSLog(@"webpage size %f",self.webview.frame.size.height); 

self.webview.frame = aFrame; 
UIGraphicsBeginImageContext([self.webview sizeThatFits:[[UIScreen mainScreen] bounds].size]); 
CGContextRef resizedContext = UIGraphicsGetCurrentContext(); 

// crash 
[self.webview.layer renderInContext:resizedContext]; // crash 
// crash 

UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 

UIGraphicsEndImageContext(); 
self.webview.frame = tmpFrame; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *pngPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",@"TestImage"]]; 
NSError *error; 
[UIImagePNGRepresentation(image) writeToFile:pngPath options:NSDataWritingAtomic error:&error]; 

NSURL *url = [NSURL fileURLWithPath:pngPath]; 

//reset webview 
self.webview.bounds = tmpBounds; 
self.webview.frame = tmpFrame; 

시뮬레이터가 충돌하지 않는, 여기에 콘솔에서 로깅 오류는 유사한 문제를 겪고있는이 동일한 문제가 발생하여 이미지의 높이가 약 8,000 픽셀을 넘는 지 먼저 계산하여 해결했습니다.

그렇다면 iPad 1 하드웨어 버전 일 때마다 페이지가 너무 커서이 프로세스에 실패했음을 사용자에게 경고했습니다.

이 문제에 대한 진정한 해결책을 찾지 못했습니다.