方法一:利用 WebView
-(void)loadDocument:(NSString *)documentName inView:(UIWebView *)webView
{
NSString *path = [[NSBundle mainBundle] pathForResource:documentName ofType:nil];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
優點:實現極為簡單。缺點:僅能瀏覽,拿不到任何回調,且為固定豎版拖動,難以實現翻頁動效。

方法二:利用 CGContextDrawPDFPage
此方式能彌補 WebView 的不足,代價是需要撰寫更多程式碼。首先封裝取得 PDF 文件與顯示單頁的函數:
CGPDFDocumentRef GetPDFDocumentRef(NSString *filename)
{
CFStringRef path;
CFURLRef url;
CGPDFDocumentRef document;
size_t count;
path = CFStringCreateWithCString(NULL, [filename UTF8String], kCFStringEncodingUTF8);
url = CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, 0);
CFRelease(path);
document = CGPDFDocumentCreateWithURL(url);
CFRelease(url);
count = CGPDFDocumentGetNumberOfPages(document);
if (count == 0) {
printf("[%s] needs at least one page!\n", [filename UTF8String]);
return NULL;
} else {
printf("[%ld] pages loaded in this PDF!\n", count);
}
return document;
}
void DisplayPDFPage(CGContextRef myContext, size_t pageNumber, NSString *filename)
{
CGPDFDocumentRef document;
CGPDFPageRef page;
document = GetPDFDocumentRef(filename);
page = CGPDFDocumentGetPage(document, pageNumber);
CGContextDrawPDFPage(myContext, page);
CGPDFDocumentRelease(document);
}
如此顯示出來的 PDF 單頁會是倒立的,這是因為 Quartz 座標系與 UIView 座標系方向不一致。調整座標系即可讓 PDF 正立:
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 80, self.frame.size.height-60);
CGContextScaleCTM(context, 1, -1);
再搭配 iOS 5 登場的 UIPageViewController,便能實現流暢的翻頁瀏覽:
- (PDFViewController *)viewControllerAtIndex:(NSUInteger)index
{
if (([self.pagePDF count] == 0) || (index > [self.pagePDF count])) {
return nil;
}
PDFViewController *dataViewController = [[PDFViewController alloc] initWithNibName:@"PDFViewController" bundle:nil];
dataViewController.pdfview = [[PDFView alloc] initWithFrame:self.view.frame atPage:index];
[dataViewController.view addSubview:dataViewController.pdfview];
return dataViewController;
}
- (NSUInteger)indexOfViewController:(PDFViewController *)viewController
{
return [self.pagePDF indexOfObject:viewController.pdfview];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSUInteger index = [self indexOfViewController:(PDFViewController *)viewController];
if ((index == 0) || (index == NSNotFound)) {
return nil;
}
index--;
return [self viewControllerAtIndex:index];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger index = [self indexOfViewController:(PDFViewController *)viewController];
if (index == NSNotFound) {
return nil;
}
index++;
if (index == [self.pagePDF count]) {
return nil;
}
return [self viewControllerAtIndex:index];
}


塗鴉功能主要涉及以下幾個要點:
1. 多 context、分層作畫
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
2. 觸摸事件(touches 系列)
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
3. 初始化單頁視圖並傳入頁碼
- (id)initWithFrame:(CGRect)frame onPage:(NSInteger)page
4. 繪製軌跡的方法
CG_EXTERN void CGPathMoveToPoint(CGMutablePathRef path, const CGAffineTransform *m, CGFloat x, CGFloat y)
CG_EXTERN void CGPathAddLineToPoint(CGMutablePathRef path, const CGAffineTransform *m, CGFloat x, CGFloat y)
透過分層繪製 PDF 頁面與塗鴉軌跡,再結合 touches 事件即時響應手指移動,便能實現流暢的 PDF 瀏覽與塗鴉體驗。


