iOS PDF的显示和浏览

来源:互联网 发布:米6 手机无法连接网络 编辑:程序博客网 时间:2024/05/22 17:45


方法一:利用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.实现简单

弊:1.仅能浏览,拿不到任何回调。

      2.固定竖版拖动,不能实现翻页动画效果



方法二:利用CGContextDrawPDFPage


gitHub: https://github.com/marujun/IBooKTurnPagesReader

  

 

CGPDFDocumentRef GetPDFDocumentRef(NSString *filename)  {      CFStringRef path;      CFURLRef url;      CGPDFDocumentRef document;      size_t count;   NSString *filePath=[[[NSBundlemainBundle] resourcePath]stringByAppendingPathComponent:filename];    path = CFStringCreateWithCString (NULL, [filePath 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); 


    转自:http://blog.csdn.net/yiyaaixuexi/article/details/7645725
    参考:http://marshal.easymorse.com/archives/3585






    原创粉丝点击