CrazyBoy 稍嫌羞澀的路人
註冊時間: 2011-08-23 文章: 2
61.14 果凍幣
|
發表於: 2011-8-23, PM 6:58 星期二 文章主題: 使用Quartz 2D讀取PDF文件 |
|
|
先建立一個普通專案,然後添加一個UIScrollView(不會添加的話再問我),命名為PDFView.
PDFView.h
代碼: |
@interface PDFView : UIScrollView {
NSString *filePath;
CGPDFDocumentRef pdfDocument;
CGPDFPageRef page;
int pageNumber;
}
@property (copy, nonatomic) NSString *filePath;
@property int pageNumber;
-(CGPDFDocumentRef)MyGetPDFDocumentRef;
-(void)reloadView;
-(IBAction)goUpPage:(id)sender;
-(IBAction)goDownPage:(id)sender;
@end
|
PDFView.m
代碼: |
@implementation PDFView
@synthesize filePath,pageNumber;
- (void)drawRect:(CGRect)rect //只要是UIView都有的繪圖函數
{
if(filePath == nil) //如果沒初始化,就初始化
{
pageNumber = 10; //定義頁數
filePath = [[NSBundle mainBundle] pathForResource:@"zhaomu" ofType:@"pdf"];
pdfDocument = [self MyGetPDFDocumentRef]; //從自定義getter得到文件索引
}
CGContextRef myContext = UIGraphicsGetCurrentContext();
page = CGPDFDocumentGetPage(pdfDocument, pageNumber);
//由頁碼+文檔得到頁碼索引
CGContextDrawPDFPage(myContext, page);
//繪製頁面
}
//此getter可以考慮照打... 都是CF函數,我看到就恶心...
- (CGPDFDocumentRef)MyGetPDFDocumentRef
{
CFStringRef path;
CFURLRef url;
CGPDFDocumentRef document;
path = CFStringCreateWithCString(NULL, [filePath UTF8String], kCFStringEncodingUTF8);
url = CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, 0);
CFRelease(path);
document = CGPDFDocumentCreateWithURL(url);
CFRelease(url);
return document;
}
-(void)reloadView
{
[self setNeedsDisplay]; //每次需要Refresh了,就call這個
}
-(IBAction)goUpPage:(id)sender
{
pageNumber++;
[self reloadView];
}
-(IBAction)goDownPage:(id)sender
{
pageNumber--;
[self reloadView];
}
@end
|
只是半成品,需要完成品的請自我研究。 |
|