iOS开发笔记——PDF的显示和浏览

今天的任务是:在iOS上加载显示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.固定竖版拖动,想实现翻页动效果就扒瞎


        


        下面的方法可以解决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];  
    }  





后续将完成涂鸦pdf后保存创建新pdf的功能。

特别感谢cclv的帮助和指点。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值