我想使用CGPDF API来显示和操作IPAD上的PDF,但首先,我尝试从简单的方式(没有屏幕调整)在我的视图上显示PDF。我不知道为什么PDF没有出现在我的应用程序中,也许我做错了什么。我正在使用NSLOG来计算页数,它确实起作用了,但是PDF没有出现,只是在我的模拟器屏幕上出现了白色背景。
下面是我的代码:
//
**// ViewController.h**
// MeuCGPDF
//
// Created by admin on 01/11/13.
// Copyright (c) 2013 admin. All right reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
----------------------------------------------------------
//
**// ViewController.m**
// MeuCGPDF
//
// Created by admin on 01/11/13.
// Copyright (c) 2013 admin. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *pathToPdfDoc = [[NSBundle mainBundle] pathForResource:@"livro" ofType:@"pdf"];
NSURL *pdfUrl = [NSURL fileURLWithPath:pathToPdfDoc];
CGPDFDocumentRef document = CGPDFDocumentCreateWithURL((CFURLRef)pdfUrl);
size_t pageCount = CGPDFDocumentGetNumberOfPages(document);
CGPDFPageRef page = CGPDFDocumentGetPage(document, 1);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawPDFPage(context, page);
NSLog(@"Paginas: %zu", pageCount);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
----------------------------------------------------------发布于 2014-02-06 18:35:57
如果您只想在web视图中显示PDF,则:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.pdfWebView.delegate=self;
[self displayPdfinWebView];
}
-(void)displayPdfinWebView {
NSString *path = [[NSBundle mainBundle] pathForResource:@"YourPDFname" ofType:@"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[self.pdfWebView loadRequest:request];
}如果你想要更多的功能,如双击缩放或挤压,我不会推荐使用WebView,因为缺乏性能和质量。有一个ZoomingPdfViewer的苹果代码,它添加了一个带有levelsOfDetail n levelsOfDetailBias的TileLayer,这对于正常的pdf大小来说是合适的。
希望这能有所帮助。
https://stackoverflow.com/questions/19726243
复制相似问题