具有非有限位置的子层[inf inf]

har*_*eng 6 ios ios9 swift2

我正在使用Objective-C编写的第三个CocoaPods库来截取UITextView的截图.对于iOS 8来说没关系,但是在我更改了iOS 9和Swift 2的语法后,它会抛出一个错误:

由于未捕获的异常'CALayerInvalidGeometry'而终止应用程序,原因:'子层具有非有限位置[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;
}
Run Code Online (Sandbox Code Playgroud)

问题是这一行:

[self.layer renderInContext:context];
Run Code Online (Sandbox Code Playgroud)

我意识到这是因为我试图拍摄包含UIScrollView的UIView的快照.如果我删除UIScrollView子视图,则错误消失.

那么这是什么意思?

具有非有限位置的子层[inf inf]

任何人都有同样的问题,你是如何解决的?

har*_*eng 8

以下是我解决这个问题的方法:

关注这篇文章

https://github.com/facebook/ios-snapshot-test-case/issues/112

错误来自我使用的库,因此搜索所有文件并查找

CGRectNull
Run Code Online (Sandbox Code Playgroud)

并将它们全部改为

CGRectZero
Run Code Online (Sandbox Code Playgroud)

这解决了这个问题.