CoreData实例分析学习(1)
Drawing a Grid in a UITableView

在iPhone程序上读取PDF文件 (rev0.1 ^-^;)

Chirpware posted @ 2009年12月16日 16:56 in iphone开发 with tags pdf iphone , 3701 阅读

 怎么在iPhone程序中读取PDF的内容呢?答案是,苹果为我们准备了一个很神奇的framework Q2D(Quartz 2D)。Q2D提供了全套的PDF读取API,接下来我们来看看如果简单的使用Q2D来读取PDF文件:

 我建立了一个工程叫iPhonePDF, 添加了一个UIScrollView(不知道怎么添加UIScrollView? 添加一个UIView然后把interface上的UIView改成UIScrollView就可以啦…)名为PDFView

看看PDFView里面有什么吧

@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

filePath是储存pdf文件的位置的,得到文件位置就是老话题了:[NSBundle mainBundle]… 后面的会写吧… 不记得了在我博客里面搜索吧

CGPDFDocumentRef是PDF文档索引文件,Q2D是Core Foundation的API,所以没看到那个星星~

CGPDFPageRef是PDF页面索引文件

pageNumber是页码

下面的几个函数其实一看就明了了,翻页的,和刷新页面的。第一个是自定义的getter

然后我们看看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函数,我看到就恶心。
//其实不是很难了,得到文件,转换成URL,然后通过
//CGPDFDocumentCreateWithURL(url)得到文件内容索引
//Ta Daaa~~
- (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]; //每次需要重画视图了,就call这个
}
 
-(IBAction)goUpPage:(id)sender
{
 pageNumber++;
 
 [self reloadView];
}
-(IBAction)goDownPage:(id)sender
{
 pageNumber--;
 [self reloadView];
}
@end

 

pavzi.com 说:
2024年1月19日 15:23

Pavzi.com provides all the news about Gadgets, the Economy, Technology, Business, Finance and many more. The main concept or our aim behind this website has been the will to provide resources with full information on each topic which can be accessed through the Internet. To ensure that every reader gets what is important and worthy about the topic they search and link to hear from us. pavzi.com Our site is a multiple Niche or category website which will ensure to provide information and resources on each and every topic. Some of the evergreen topics you will see on our website are Career, Job Recruitment, Educational, Technology, Reviews and others. We are targeting mostly so it is true that Tech, Finance, and Product Reviews. The only reason we have started this website is to make this site the need for your daily search use.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter