Objective c 使用CGPDFDocumentGetNumberOfPages显示总页数
我想在导航栏上显示pdf格式的总页数Objective c 使用CGPDFDocumentGetNumberOfPages显示总页数,objective-c,pdf,ios6,Objective C,Pdf,Ios6,我想在导航栏上显示pdf格式的总页数 - (void)viewDidLoad { [super viewDidLoad]; NSString *path = [[NSBundle mainBundle] pathForResource:@"pages" ofType:@"pdf"]; totalPages = (int)CGPDFDocumentGetNumberOfPages(path); PageViewController *page = [[PageViewController a
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"pages" ofType:@"pdf"];
totalPages = (int)CGPDFDocumentGetNumberOfPages(path);
PageViewController *page = [[PageViewController alloc] initWithPDFAtPath:path];
page.view.frame = self.view.bounds;
[self.view addSubview:page.view];
[self addChildViewController:page];
[self displaycurrentIndex:1];
}
- (void) displaycurrentIndex:(NSUInteger)currentIndex {
self.navigationItem.title = [NSString stringWithFormat:
@"Page %u of %u",
currentIndex,
totalPages];
}
但导航栏上显示的是0中的1
然而,它应该是pdf格式的totalpages的1
编辑:
我没有像这样使用URL语句
CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("paper.pdf"), NULL, NULL);
pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
就我而言,我正在使用
NSString *path = [[NSBundle mainBundle] pathForResource:@"pages" ofType:@"pdf"];
这就是为什么它无法获得pdf格式的总页数。如何使用我的案例获取pdf中的总页数
感谢
CGPDFDocumentGetNumberOfPages()
需要CGPDFDocumentRef
作为参数,而不是路径
到文件中
以下代码可以在给定的路径打开PDF文档
和
获取页数:
NSURL *pdfurl = [NSURL fileURLWithPath:path];
CGPDFDocumentRef pdfDocument = CGPDFDocumentCreateWithURL((__bridge CFURLRef)pdfurl);
int totalPages = (int)CGPDFDocumentGetNumberOfPages(pdfDocument);
CGPDFDocumentRelease(pdfDocument);
((\uu桥CFURLRef)
仅在使用ARC编译时才是必需的。)
嗯?您需要传入一个CGPDFDocumentRef
,而不是文件系统路径
()我知道以前我的CGPDFDocumentRef是pdf格式的-(void)displayPageNumber:(nsInteger)pageNumber{self.navigationItem.title=[NSString stringWithFormat:@“第%u页,共%u页”,pageNumber,CGPDFDocumentGetNumberOfPages(pdf)];}但它仍然没有显示pdf页面的总页数,它是一个位为NSString*path=[[NSBundle mainBundle]pathForResource:@“pages”of type:@“pdf”];NSURL*pdfurl=[NSURL fileURLWithPath:path];pdf=CGPDFDocumentCreateWithURL((uu桥CFURLRef)pdfurl);-(void)displaycurrentIndex:(nsInteger)currentIndex{self.navigationItem.title=[NSString stringWithFormat:@“第%i页,共%i页”,currentIndex,CGPDFDDocumentGetNumberOfPages(pdf)];}现在它开始工作了。谢谢你的帮助。@user1120133:不客气。(请记住在某处发布PDF文档。)我正在使用ARC。它将自动发布PDF文档。@ USE1120133:ARC不释放核心基础对象,如CGPDFDCONTRONTROR.REST。谢谢分享。那我就放了。再次感谢您的时间和帮助。谢谢。
totalPages = (int)CGPDFDocumentGetNumberOfPages(path);