在Apple TV tvOS中呈现PDF
我正在为我的tvOS应用程序添加一个功能,允许查看应用程序中存储的PDF。然而,如果没有UIWebView,我就不知道如何做到这一点。我在其他地方问过这个问题,苹果公司给我提供了一个关于可以使用的API的冗长而无助的文档链接,即使在这里,它也被引用了(CGPDFPage),但没有关于如何实现这一点的真正指南。有人在tvOS上成功地完成了这项工作吗?如果有,您能帮助我开始这项工作吗?tvOS文档中有一节介绍了这项工作,因此我认为它包含了您需要的功能 该页面上有很多示例代码,但这里有一些我在iOS上使用的代码,用于相同的目的。它应该在tvOS上工作,但我没有办法测试它:在Apple TV tvOS中呈现PDF,pdf,tvos,Pdf,Tvos,我正在为我的tvOS应用程序添加一个功能,允许查看应用程序中存储的PDF。然而,如果没有UIWebView,我就不知道如何做到这一点。我在其他地方问过这个问题,苹果公司给我提供了一个关于可以使用的API的冗长而无助的文档链接,即使在这里,它也被引用了(CGPDFPage),但没有关于如何实现这一点的真正指南。有人在tvOS上成功地完成了这项工作吗?如果有,您能帮助我开始这项工作吗?tvOS文档中有一节介绍了这项工作,因此我认为它包含了您需要的功能 该页面上有很多示例代码,但这里有一些我在iOS上
func imageForPDF(URL: NSURL, pageNumber: Int, imageWidth: CGFloat) -> UIImage {
let document = CGPDFDocumentCreateWithURL(URL)
let page = CGPDFDocumentGetPage(document, pageNumber)
var pageRect = CGPDFPageGetBoxRect(page, .MediaBox)
let scale = imageWidth / pageRect.size.width
pageRect.size = CGSizeMake(pageRect.size.width * scale, pageRect.size.height * scale)
pageRect.origin = CGPointZero
UIGraphicsBeginImageContext(pageRect.size)
let ctx = UIGraphicsGetCurrentContext()
CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0, 1.0) // White background
CGContextFillRect(ctx, pageRect)
CGContextSaveGState(ctx)
// Rotate the PDF so that it’s the right way around
CGContextTranslateCTM(ctx, 0.0, pageRect.size.height)
CGContextScaleCTM(ctx, 1.0, -1.0)
CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform(page, .MediaBox, pageRect, 0, true))
CGContextDrawPDFPage(ctx, page)
CGContextRestoreGState(ctx)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
下面是我在tvOS中编写和测试的一些代码。请注意,这在Objective-c中 我创建了两个函数来完成这项工作,还有一个助手函数在UIScrollView中显示PDF图像。第一个将从URL打开PDF文档。使用了一个web URL。本示例中还可以使用本地文件 还有一个帮助函数,用于从本地文件打开文档 第二个函数将PDF文档呈现到上下文中。我选择通过创建一个图像来显示上下文。还有其他处理上下文的方法 打开文档相当简单,因此代码中没有对此的注释。呈现文档要稍微复杂一些,因此有注释解释该函数 完整的应用程序如下
- (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;
}
}
请注意,我使用的是一个在网上免费提供的随机PDF。我在使用https URL时遇到了一些问题,但我相信这是可以解决的,而且实际上与PDF开头的问题无关。您收到的链接是什么?拥有CGPDFPage通常对您没有帮助,因为它只包含关于页面的“元数据”信息,但它本身无法呈现其内容。它通常是CGContext,它能够获取并呈现这样一个页面;但这很可能是API不可用的原因之一(请参见此处,如果是:)谢谢。我回来后会试一试的。大多数PDF都应该存储在应用程序中(我想我可以把它放进去,但仍然低于200Mb的限制)快速提问…我现在在商店里有了这个应用程序,只需使用视频功能和TVML应用程序。要像这样运行它,它需要是一个定制的应用程序吗?不幸的是,这不会真正起作用,因为这个项目中涉及的几乎每个PDF都有多个页面。我已经编辑了使用多页面文档的代码。新代码使用数组存储每个页面的图像。然后创建UIScrollView以显示所有图像。请注意,如果您使用的是自动布局(您应该这样做),那么关于让UIScrollView随着内容的增加而增加有一些细节。如果你做不好,请告诉我,我会尽力帮助你。我想说的是,你可以用许多不同的方式显示UIImage数组(文档的页面),这取决于你要看的演示文稿。UIPageViewController将是另一种显示它们的好方法。它可能比使用UIScrollView要复杂一些,但结果会更好。
CGPDFDocumentRef pdfDocument = [self openPDFURL:@"http://www.guardiansuk.com/uploads/accreditation/10testing.pdf"];
[self drawDocument:pdfDocument];