我正在研究添加到我的tvOS应用程序,以便查看存储在应用程序中的PDF.但是,如果没有UIWebView,我会对如何做到这一点感到茫然.我在其他地方问了一个问题,并且收到一条链接,指向Apple提供的关于可以使用的API的冗长而无助的文档,甚至在这里它已被引用(CGPDFPage),但没有关于如何实现这个的实际指南.有没有人在tvOS上成功完成此操作,如果是这样,你会帮助我开始这个过程吗?
下面是我在tvOS中编写和测试的一些代码.请注意,这是在Objective-c中.
我创建了两个函数来完成这项工作,还有一个帮助函数在UIScrollView中显示PDF图像.第一个将从URL打开PDF文档.使用了网址.本示例中也可以使用本地文件.
还有一个辅助函数可以从本地文件打开文档.
第二个函数将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;
}
}
Run Code Online (Sandbox Code Playgroud)
要将它们组合在一起,您可以这样做:
CGPDFDocumentRef pdfDocument = [self openPDFURL:@"http://www.guardiansuk.com/uploads/accreditation/10testing.pdf"];
[self drawDocument:pdfDocument];
Run Code Online (Sandbox Code Playgroud)
请注意,我使用的是随机PDF,可以在网上免费获得.我遇到了一些https网址问题,但我确信这可以解决,而且它实际上与PDF开放问题无关.
| 归档时间: |
|
| 查看次数: |
1216 次 |
| 最近记录: |