在iOS 8中保存为PDF的奇怪行为

时间:2014-10-29 18:49:38

标签: ios objective-c uiscrollview ios8

我一直在使用以下代码将指定的UIScrollView保存为.pdf文件。

+ (NSString *) saveAsPDF:(UIScrollView *)view withName:(NSString *)filename
{
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentDirectory = [path objectAtIndex:0];
    NSString *pdfPathWithFileName = [documentDirectory stringByAppendingPathComponent:filename];

    // Generate PDF
    {
        UIGraphicsBeginPDFContextToFile(pdfPathWithFileName, CGRectZero, nil);
        UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, view.contentSize.width, view.contentSize.height), nil);

        // My Draw View
        {
            NSArray *subviews = [view subviews];
            UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.contentSize.width, view.contentSize.height)];

            for (UIView *viewLoop in subviews)
            {
                [contentView addSubview:viewLoop];
            }

            CGContextRef context = UIGraphicsGetCurrentContext();
            [contentView setBackgroundColor:[self colorForBlueBackground]];
            [contentView.layer renderInContext:context];

            for (UIView *viewLoop in [contentView subviews])
            {
                [viewLoop removeFromSuperview];
                [view addSubview:viewLoop];
            }
        }
        UIGraphicsEndPDFContext();

        return pdfPathWithFileName;
    }
}

这已经好几个月了,但是在更新到iOS 8之后,它一直表现得很奇怪。似乎在UIGraphicsEndPDFContext()行上有一个假想的断点,好像发生了运行时错误,但是在屏幕上或Debug区域的Console窗格中没有弹出错误消息。我必须点击“继续执行程序”#39;按钮几次,然后程序继续执行。

之后,文件似乎被正确保存,但屏幕上的UIScrollView向左移动了大约一半的屏幕宽度,屏幕的右半部分只是空白。

有谁知道iOS 8中的变化让我得到了这些幻像断点和奇怪的UI行为?

0 个答案:

没有答案