ios教程lwxshow.com-PDF的显示和浏览

来源:互联网 发布:自己制作城市地图软件 编辑:程序博客网 时间:2024/05/16 01:27

转自:http://lwxshow.com/lwxshow-com-pdf-ios-tutorial-shows

1.使用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];}


2.使用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坐标系不一样所致,调整坐标系,修改代码如下:

    CGContextRef context = UIGraphicsGetCurrentContext();    CGContextTranslateCTM(context, 80, self.frame.size.height-60);    CGContextScaleCTM(context, 1, -1);


效果如下:

 


原创粉丝点击