iOS下PDF文件的浏览和涂鸦效果的简单实现

   2015-10-29 0
核心提示:这篇文章主要介绍了iOS下PDF文件的浏览和涂鸦效果的简单实现,代码基于传统的Objective-C,需要的朋友可以参考下

浏览PDF的效果
方法一:利用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]; 


利:1.实现简单
        2.还是实现简单
弊:1.仅能浏览,拿不到任何回调,safari不会鸟任何人。
        2.固定竖版拖动,想实现翻页动效果就扒瞎

iOS下PDF文件的浏览和涂鸦效果的简单实现

下面的方法可以解决webview 显示pdf的弊,相对的,要付出一些汗水作为代价了。

方法二:利用CGContextDrawPDFPage

复制代码 代码如下:

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); 


配合iOS5强大的UIPageViewController实现翻页浏览

复制代码 代码如下:

- (PDFViewController *)viewControllerAtIndex:(NSUInteger)index  

    //Return the PDFViewController for the given index. 
    if (([self.pagePDF count] == 0 )|| (index > [self.pagePDF count]) ) { 
        return nil; 
    } 
     
    //Create a new view controller and pass suitable data. 
    PDFViewController *dataViewController = [[PDFViewController alloc]initWithNibName:@"PDFViewController" bundle:nil]; 
    //dataViewController.pdfview = [self.pagePDF objectAtIndex:index]; 
    dataViewController.pdfview = [[PDFView alloc]initWithFrame:self.view.frame atPage:index]; 
    [dataViewController.view addSubview:dataViewController.pdfview]; 
    NSLog(@"index = %d",index); 
    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]; 


iOS下PDF文件的浏览和涂鸦效果的简单实现

iOS下PDF文件的浏览和涂鸦效果的简单实现

涂鸦效果
主要涉及:
1. 多context,分层画画

复制代码 代码如下:

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx


2. 触摸事件touches族那些event

复制代码 代码如下:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
……


3. 初始化单页view传页码

复制代码 代码如下:

- (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)


iOS下PDF文件的浏览和涂鸦效果的简单实现

iOS下PDF文件的浏览和涂鸦效果的简单实现

iOS下PDF文件的浏览和涂鸦效果的简单实现

iOS下PDF文件的浏览和涂鸦效果的简单实现

 
标签: iOS PDF
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

点击排行