UIWebView 全体 (画面外のコンテンツも含む)を PNGにキャプチャしようとしています。次のコードはほとんどのデバイスで問題なく動作しますが、一部の iPad 第 1 世代ではこの行でクラッシュしました。
[self.webview.layer renderInContext:resizedContext]; /// crash
renderInContext プロセスを停止してクラッシュを防ぐにはどうすればよいですか?
問題のコンテキストについては、以下のコード サンプルを参照してください。
//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;
シミュレーターはクラッシュしません。デバイスで実行した場合のコンソールからのログ エラーは次のとおりです。
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";
}