且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Apple TV tvOS中渲染PDF

更新时间:2023-12-03 20:43:46

下面是我在tvOS中编写和测试的一些代码.请注意,这是在Objective-c中.

Below is some code that I wrote and tested in tvOS. Note that this is in Objective-c.

我创建了两个函数来完成这项工作,并创建了一个辅助函数来在UIScrollView中显示PDF图像.第一个将从URL打开PDF文档.使用了Web URL.此示例中也可以使用本地文件.

I've created two functions to do the job, and one helper function to display the PDF images in a UIScrollView. The first one will open the PDF document from a URL. A web URL was used. A local file could also be used in this sample.

还有一个帮助程序功能,可以从本地文件打开文档.

There is also a helper function to open a document from a local file.

第二个功能将PDF文档呈现到上下文中.我选择通过从中创建图像来显示上下文.还有其他处理上下文的方法.

The second function renders the PDF document to a context. I chose to display the context by creating an image from it. There are other ways of handling the context too.

打开文档非常简单,因此在代码中没有注释.呈现文档的过程稍微复杂一些,因此有一些注释可以解释该功能.

Opening the document is fairly straight forward, so there are no comments in the code for that. Rendering the document is slightly more involved, and so there are comments explaining that function.

完整的应用程序在下面.

The complete application is below.

- (CGPDFDocumentRef)openPDFLocal:(NSString *)pdfURL {
    NSURL* NSUrl = [NSURL fileURLWithPath:pdfURL];

    return [self openPDF:NSUrl];
}

- (CGPDFDocumentRef)openPDFURL:(NSString *)pdfURL {
    NSURL* NSUrl= [NSURL URLWithString:pdfURL];

    return [self openPDF:NSUrl];
}

- (CGPDFDocumentRef)openPDF:(NSURL*)NSUrl {
    CFURLRef url = (CFURLRef)CFBridgingRetain(NSUrl);

    CGPDFDocumentRef myDocument;
    myDocument = CGPDFDocumentCreateWithURL(url);
    if (myDocument == NULL) {
        NSLog(@"can't open %@", NSUrl);
        CFRelease (url);
        return nil;
    }
    CFRelease (url);

    if (CGPDFDocumentGetNumberOfPages(myDocument) == 0) {
        CGPDFDocumentRelease(myDocument);
        return nil;
    }

    return myDocument;
}
- (void)drawDocument:(CGPDFDocumentRef)pdfDocument
{
    // Get the total number of pages for the whole PDF document
    int  totalPages= (int)CGPDFDocumentGetNumberOfPages(pdfDocument);
    NSMutableArray *pageImages = [[NSMutableArray alloc] init];

    // Iterate through the pages and add each page image to an array
    for (int i = 1; i <= totalPages; i++) {
        // Get the first page of the PDF document
        CGPDFPageRef page = CGPDFDocumentGetPage(pdfDocument, i);
        CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);

        // Begin the image context with the page size
        // Also get the grapgics context that we will draw to
        UIGraphicsBeginImageContext(pageRect.size);
        CGContextRef context = UIGraphicsGetCurrentContext();

        // Rotate the page, so it displays correctly
        CGContextTranslateCTM(context, 0.0, pageRect.size.height);
        CGContextScaleCTM(context, 1.0, -1.0);
        CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(page, kCGPDFMediaBox, pageRect, 0, true));

        // Draw to the graphics context
        CGContextDrawPDFPage(context, page);

        // Get an image of the graphics context
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        [pageImages addObject:image];
    }
    // Set the image of the PDF to the current view
    [self addImagesToScrollView:pageImages];
}

-(void)addImagesToScrollView:(NSMutableArray*)imageArray {
    int heigth = 0;
    for (UIImage *image in imageArray) {
        UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
        imgView.frame=CGRectMake(0, heigth, imgView.frame.size.width, imgView.frame.size.height);
        [_scrollView addSubview:imgView];
        heigth += imgView.frame.size.height;
    }
}

并将其捆绑在一起,您可以执行以下操作:

And to tie it all together, you can do this:

CGPDFDocumentRef pdfDocument = [self openPDFURL:@"http://www.guardiansuk.com/uploads/accreditation/10testing.pdf"];
[self drawDocument:pdfDocument];

请注意,我使用的是随机PDF,该PDF在网络上免费提供.我在使用https URL时遇到了一些问题,但是我敢肯定这可以解决,并且实际上与PDF开头问题无关.

Note that I'm using a random PDF that was available for free on the web. I ran into some problems with https URLs, but I'm sure this can be resolved, and it's not actually related to the PDF opening question.