在Apple TV tvOS中渲染PDF [英] Rendering PDF in Apple TV tvOS

查看:96
本文介绍了在Apple TV tvOS中渲染PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的tvOS应用程序添加一项功能,该功能允许查看存储在该应用程序中的PDF.但是,如果没有UIWebView,我将无所适从.我曾在其他地方问过问题,并获得了指向苹果公司冗长且无助的文档的链接,这些文档可以使用的API,即使在这里也已被引用(CGPDFPage),但没有有关如何实现此API的真正指南. .有没有人在tvOS上成功完成此操作,如果是,您是否可以帮助我开始这一过程?

I am working on an addition to my tvOS app that would allow viewing of PDFs stored in the app. However, without UIWebView, I'm at a loss on how to do this. I've asked question in other places, and get greeted with a link to a wordy and helpless document from Apple about the APIs that can be used, and even here it has been referenced (CGPDFPage) but no real guide on how to implement this. Has anyone successfully done this on tvOS, and if so, would you help me get started in this process?

推荐答案

下面是我在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.

这篇关于在Apple TV tvOS中渲染PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆