【发布时间】:2015-10-03 04:53:55
【问题描述】:
我正在使用第三个用 Objective-C 编写的 CocoaPods 库来截取 UITextView 的屏幕截图。 iOS 8 没问题,但是在我更改 iOS 9 和 Swift 2 的语法后,它会抛出一个错误:
由于未捕获的异常 'CALayerInvalidGeometry' 导致应用程序终止,原因:'sublayer with non-finite position [inf inf]'
这是库中的代码:
- (UIImage *)screenshotForCroppingRect:(CGRect)croppingRect
{
UIGraphicsBeginImageContextWithOptions(croppingRect.size, NO, [UIScreen mainScreen].scale);
// Create a graphics context and translate it the view we want to crop so
// that even in grabbing (0,0), that origin point now represents the actual
// cropping origin desired:
CGContextRef context = UIGraphicsGetCurrentContext();
if (context == NULL) return nil;
NSLog(@"hiii :%f" , croppingRect.size.width);
CGContextTranslateCTM(context, -croppingRect.origin.x, -croppingRect.origin.y);
[self layoutIfNeeded];
[self.layer renderInContext:context];
UIImage *screenshotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return screenshotImage;
}
问题出在这一行:
[self.layer renderInContext:context];
我意识到这是因为我正在尝试拍摄包含 UIScrollView 的 UIView 的快照。如果我删除 UIScrollView 子视图,错误就消失了。
那么这是什么意思呢?
具有非有限位置的子层 [inf inf]
任何人有同样的问题,你是如何解决的?
【问题讨论】: