iPhone開發(fā)應用中PDF案例實現(xiàn)
作者:佚名
iPhone開發(fā)應用中PDF案例實現(xiàn)是本文要介紹的內(nèi)容,主要是來學習iPhone開發(fā)中PDF的解析內(nèi)容,文章內(nèi)容不多,主要是基于代碼來實現(xiàn)。來看詳細內(nèi)容。
iPhone開發(fā)應用中PDF案例實現(xiàn)是本文要介紹的內(nèi)容,主要是來學習iPhone開發(fā)中PDF的解析內(nèi)容,文章內(nèi)容不多,主要是基于代碼來實現(xiàn)。來看詳細內(nèi)容。
- #import <UIKit/UIKit.h>
- @class PDFTestViewController;
- @interface PDFView : UIView {
- //這個類封裝了PDF畫圖得所有信息
- CGPDFDocumentRef pdf;
- //PDFDocument 中得一頁
- CGPDFPageRef page;
- //總共頁數(shù)
- int totalPages;
- //當前得頁面
- int currentPage;
- PDFTestViewController *pdftest;
- }
- @property(nonatomic,retain)IBOutlet PDFTestViewController *pdftest;
- //當前視圖初始化類,在該方法中會創(chuàng)建一個CGPDFDocuemntRef對象,傳遞一個PDF文件得名字,和所需要頁面得大小,
- - (id)initWithFrame:(CGRect)frame andFileName:(NSString *)fileName;
- //創(chuàng)建一個PDF對象,此方法在初始化方法中被調(diào)用
- - (CGPDFDocumentRef)createPDFFromExistFile:(NSString *)aFilePath;
- -(void)reloadView;
- /*
- 頁面之間得跳轉(zhuǎn)
- */
- -(void)goUpPage;
- -(void)goDownPage;
- @end
- //
- // PDFView.m
- // PDFViewTest
- //
- // Created by Evan Lynn on 10-6-20.
- // Copyright 2010 Tera Age. All rights reserved.
- //
- #import "PDFView.h"
- //#import "PDFTestViewController.h"
- @implementation PDFView
- @synthesize pdftest;
- - (id)initWithFrame:(CGRect)frame andFileName:(NSString *)fileName{
- if (self = [super initWithFrame:frame]) {
- NSString *dataPathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];
- pdf = [self createPDFFromExistFile:dataPathFromApp];
- self.backgroundColor = [UIColor clearColor];
- }
- return self;
- }
- - (CGPDFDocumentRef)createPDFFromExistFile:(NSString *)aFilePath{
- CFStringRef path;
- CFURLRef url;
- CGPDFDocumentRef document;
- path = CFStringCreateWithCString(NULL, [aFilePath UTF8String], kCFStringEncodingUTF8);
- url = CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, NO);
- CFRelease(path);
- document = CGPDFDocumentCreateWithURL(url);
- CFRelease(url);
- totalPages = CGPDFDocumentGetNumberOfPages(document);
- currentPage=1;
- if (totalPages == 0) {
- return NULL;
- }
- return document;
- }
- - (void)drawRect:(CGRect)rect {
- //得到繪圖上下文環(huán)境
- CGContextRef context = UIGraphicsGetCurrentContext();
- //得到一個PDF頁面
- page = CGPDFDocumentGetPage(pdf, currentPage);
- /*進行坐標轉(zhuǎn)換向右移動100個單位,并且向下移動當前視圖得高度,
- 這是因為Quartz畫圖得坐標系統(tǒng)是以左下角為開始點,但iphone視圖是以左上角為開始點
- */
- CGContextTranslateCTM(context, 100.0,self.bounds.size.height);
- //轉(zhuǎn)變坐標系
- CGContextScaleCTM(context, 1.0, -1);
- CGContextDrawPDFPage(context, page);
- }
- - (void)dealloc {
- [super dealloc];
- }
- -(void)reloadView{
- [self setNeedsDisplay];
- }
- -(void)goUpPage{
- if(currentPage < 2)
- return;
- --currentPage;
- [self reloadView];
- }
- -(void)goDownPage{
- if(currentPage >=totalPages)
- return;
- ++currentPage;
- [self reloadView];
- }
- @end
小結(jié):iPhone開發(fā)應用中PDF案例實現(xiàn)的內(nèi)容介紹完了,希望通過本文的學習能對你有所幫助!
責任編輯:zhaolei
來源:
互聯(lián)網(wǎng)