iPhone開(kāi)發(fā)應(yīng)用中PDF案例實(shí)現(xiàn)
作者:佚名
iPhone開(kāi)發(fā)應(yīng)用中PDF案例實(shí)現(xiàn)是本文要介紹的內(nèi)容,主要是來(lái)學(xué)習(xí)iPhone開(kāi)發(fā)中PDF的解析內(nèi)容,文章內(nèi)容不多,主要是基于代碼來(lái)實(shí)現(xiàn)。來(lái)看詳細(xì)內(nèi)容。
iPhone開(kāi)發(fā)應(yīng)用中PDF案例實(shí)現(xiàn)是本文要介紹的內(nèi)容,主要是來(lái)學(xué)習(xí)iPhone開(kāi)發(fā)中PDF的解析內(nèi)容,文章內(nèi)容不多,主要是基于代碼來(lái)實(shí)現(xiàn)。來(lái)看詳細(xì)內(nèi)容。
- #import <UIKit/UIKit.h>
- @class PDFTestViewController;
- @interface PDFView : UIView {
- //這個(gè)類(lèi)封裝了PDF畫(huà)圖得所有信息
- CGPDFDocumentRef pdf;
- //PDFDocument 中得一頁(yè)
- CGPDFPageRef page;
- //總共頁(yè)數(shù)
- int totalPages;
- //當(dāng)前得頁(yè)面
- int currentPage;
- PDFTestViewController *pdftest;
- }
- @property(nonatomic,retain)IBOutlet PDFTestViewController *pdftest;
- //當(dāng)前視圖初始化類(lèi),在該方法中會(huì)創(chuàng)建一個(gè)CGPDFDocuemntRef對(duì)象,傳遞一個(gè)PDF文件得名字,和所需要頁(yè)面得大小,
- - (id)initWithFrame:(CGRect)frame andFileName:(NSString *)fileName;
- //創(chuàng)建一個(gè)PDF對(duì)象,此方法在初始化方法中被調(diào)用
- - (CGPDFDocumentRef)createPDFFromExistFile:(NSString *)aFilePath;
- -(void)reloadView;
- /*
- 頁(yè)面之間得跳轉(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();
- //得到一個(gè)PDF頁(yè)面
- page = CGPDFDocumentGetPage(pdf, currentPage);
- /*進(jìn)行坐標(biāo)轉(zhuǎn)換向右移動(dòng)100個(gè)單位,并且向下移動(dòng)當(dāng)前視圖得高度,
- 這是因?yàn)镼uartz畫(huà)圖得坐標(biāo)系統(tǒng)是以左下角為開(kāi)始點(diǎn),但iphone視圖是以左上角為開(kāi)始點(diǎn)
- */
- CGContextTranslateCTM(context, 100.0,self.bounds.size.height);
- //轉(zhuǎn)變坐標(biāo)系
- 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開(kāi)發(fā)應(yīng)用中PDF案例實(shí)現(xiàn)的內(nèi)容介紹完了,希望通過(guò)本文的學(xué)習(xí)能對(duì)你有所幫助!
責(zé)任編輯:zhaolei
來(lái)源:
互聯(lián)網(wǎng)