从UIScrollView创建PDF(第1页后的页面为空白)

时间:2015-02-10 19:56:23

标签: ios swift pdf uiscrollview

我一直在筹码堆上,谷歌试图找到正确的方法来解决这个问题,任何帮助都会很棒。

我可以在PDF中呈现第一页,但每隔一页只是空白。

我在故事板中的视图控制器上有一个UIScrollView。我在视图控制器的viewDidLoad方法中动态地将内容加载到该滚动视图中。添加任何项目后,我记录它的高度,然后相应地设置滚动视图的内容大小高度,因此滚动视图很好。它的内容宽度设置为612

然后我调用一个函数来创建PDF:

func createPDF() {

    let pdfData = NSMutableData()

    let scrollHeigt = scrollViewForContent.contentSize.height

    let rawNumberOfPages = scrollHeigt / CGFloat(792)
    let numberOfPages = Int(ceil(rawNumberOfPages))

    var pageNumber = Int()

    let pageSize = CGSizeMake(612, 792)

    UIGraphicsBeginPDFContextToData(pdfData, CGRectZero, nil)
    let pdfContext : CGContextRef = UIGraphicsGetCurrentContext()

    do {

        UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil)

        if pageNumber < 1 {

            scrollViewForContent.layer.renderInContext(pdfContext)

        } else if pageNumber >= 1 {

            let offsetForScroll = CGFloat(pageNumber * 792)

            scrollViewForContent.setContentOffset(CGPointMake(0, offsetForScroll), animated: false)

            CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, -offsetForScroll)

            scrollViewForContent.layer.renderInContext(pdfContext)

        }

        pageNumber++

    }
        while pageNumber < numberOfPages

    UIGraphicsEndPDFContext()

    let tempDir = NSTemporaryDirectory()

    let tempDirWithFilename = tempDir.stringByAppendingPathComponent("test1.pdf")

    pdfData.writeToFile(tempDirWithFilename, atomically: true)

    let aURL : NSURL = NSURL(fileURLWithPath: tempDirWithFilename)!

    let objectsToShare : NSArray = [aURL]

    let activityVc = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)

    self.presentViewController(activityVc, animated: true, completion: nil)

}

为什么第一页以外的任何数据都没有呈现?我得到正确的页数,只有第一页有任何数据。谢谢(我是新人,真的很感谢你花时间去看)。

2 个答案:

答案 0 :(得分:3)

您应该使用以下函数将PDF上下文的变换矩阵(CTM)转换为与UIScrollView实例滚动相同的量:

CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, -offsetForScroll);

然后,您对renderInContext的调用应正常运作。

您可以通过查看以下示例来查看此方法,该示例获取特定偏移处的可见区域的屏幕截图并将其保存到图像中:

scrollViewForContent.setContentOffset(CGPoint(x: 0, y: shiftAmount), animated: false)

UIGraphicsBeginImageContextWithOptions(scrollViewForContent.frame.size, true, 0);

CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, -scrollViewForContent.contentOffset.y);

scrollViewForContent.layer.renderInContext(UIGraphicsGetCurrentContext())

let img = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

此外,您的if条件不正确。将else if pageCount > 1更改为else if pageCount >= 1,以便它不会跳过第1页。

答案 1 :(得分:0)

通常我使用以下代码从我的UIView制作PDF文件。尝试它可能是有帮助的

 // Creates a mutable data object for updating with binary data, like a byte array
        NSMutableData *pdfData = [NSMutableData data];
        UIGraphicsBeginPDFContextToData(pdfData, view.bounds, nil);

        //Start your first page
        UIGraphicsBeginPDFPage();
        CGContextRef pdfContext = UIGraphicsGetCurrentContext();

        //Now render view with subviews to PDF
        [view.layer renderInContext:pdfContext];

        // Close PDF Render
         UIGraphicsEndPDFContext();

例如

-(NSMutableData *)createPDFDatafromUIView:(UIView*)aView
{
    // Creates a mutable data object for updating with binary data, like a byte array
    NSMutableData *pdfData = [NSMutableData data];
    // Points the pdf converter to the mutable data object and to the UIView to be converted
    Markup *markup;
    for(UIView *view in [aView subviews])
    {
        if([view isKindOfClass:[Markup class]])
        {
            markup=(Markup*)view;
            break;
        }
    }

    UIGraphicsBeginPDFContextToData(pdfData, markup.bounds, nil);

    for(UIView *view in [aView subviews])
    {
        if([view isKindOfClass:[Markup class]])
        {
            markup=(Markup*)view;



            UIGraphicsBeginPDFPage();
            CGContextRef pdfContext = UIGraphicsGetCurrentContext();

            // draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData
            [markup.layer renderInContext:pdfContext];

        }
    }

    // remove PDF rendering context
    UIGraphicsEndPDFContext();
    return pdfData;
}
相关问题