您的位置:首页 > 移动开发 > IOS开发

iOS 关于展示PDF文件处理

2015-06-10 10:22 507 查看
展示PDF文件有两方式

1.用WebView展示

这种方法没什么好说的,除了样式不好看,就是简单

NSString *path = [[NSBundle mainBundle] pathForResource:@“XXX.pdf” ofType:nil];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];


2.用QuartzCore画出来

画到View 上

- (id)initWithFrame:(CGRect)frame atPage:(NSUInteger)index{

if ((self = [super initWithFrame:frame]))
{
// Initialization code
if(self != nil)
{
CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("Swift.pdf"), NULL, NULL);
pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
CFRelease(pdfURL);
}

}
return self;
}


重写drawRect: 方法。

这里的self。pageNumber 是pdf对应的页数

-(void)drawInContext:(CGContextRef)context
{

// PDF page drawing expects a Lower-Left coordinate system, so we flip the coordinate system
// before we start drawing.
CGContextTranslateCTM(context, 0.0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);

// Grab the first PDF page
CGPDFPageRef page = CGPDFDocumentGetPage(pdf, self.pageNumber);
// We’re about to modify the context CTM to draw the PDF page where we want it, so save the graphics state in case we want to do more drawing
CGContextSaveGState(context);
// CGPDFPageGetDrawingTransform provides an easy way to get the transform for a PDF page. It will scale down to fit, including any
// base rotations necessary to display the PDF page correctly.
CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, self.bounds, 0, true);
// And apply the transform.
CGContextConcatCTM(context, pdfTransform);
// Finally, we draw the page and restore the graphics state for further manipulations!
CGContextDrawPDFPage(context, page);
CGContextRestoreGState(context);
}

- (void)drawRect:(CGRect)rect {
[self drawInContext:UIGraphicsGetCurrentContext()];
}


用一个UIPageViewControl 就可以实现 翻书预览的效果

这里关于UIPageViewControl的用法参考:点击打开链接

上代码:

- (void)setPageView
{
[self createContentPages];// 初始化所有数据

// 设置UIPageViewController的配置项

NSDictionary *options =[NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:UIPageViewControllerSpineLocationMin] forKey: UIPageViewControllerOptionSpineLocationKey];

// 实例化UIPageViewController对象,根据给定的属性

self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl  navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal   options: options];

// 设置UIPageViewController对象的代理

_pageController.dataSource = self;

// 定义“这本书”的尺寸

[[_pageController view] setFrame:[[self view] bounds]];

// 让UIPageViewController对象,显示相应的页数据。

// UIPageViewController对象要显示的页数据封装成为一个NSArray。

// 因为我们定义UIPageViewController对象显示样式为显示一页(options参数指定)。

// 如果要显示2页,NSArray中,应该有2个相应页数据。

MoreViewController *initialViewController =[self viewControllerAtIndex:0];// 得到第一页

NSArray *viewControllers =[NSArray arrayWithObject:initialViewController];

[_pageController setViewControllers:viewControllers
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:nil];

// 在页面上,显示UIPageViewController对象的View

[self addChildViewController:_pageController];

[[self view] addSubview:[_pageController view]];
}


// 初始化所有数据

- (void) createContentPages {

self.loadTimes = 1;

self.pageContent = [NSMutableArray array];
for (int i = 0; i<loadMaxPage; i++) {
PDFView *paf =[[PDFView alloc]initWithFrame:self.view.frame atPage:i];

[self.pageContent addObject:paf];
}
}


// 得到相应的VC对象

- (MoreViewController *)viewControllerAtIndex:(NSUInteger)index {
if (([self.pageContent count] == 0) || (index >= [self.pageContent count])) {
return nil;
}

// 创建一个新的控制器类,并且分配给相应的数据

MoreViewController *dataViewController =[[MoreViewController alloc] init];
dataViewController.pdfView = [self.pageContent objectAtIndex:index];
[dataViewController.view addSubview:dataViewController.pdfView];
return dataViewController;

}

// 根据数组元素值,得到下标值

- (NSUInteger)indexOfViewController:(MoreViewController *)viewController {
return [self.pageContent indexOfObject:viewController.pdfView];
}

#pragma mark- UIPageViewControllerDataSource

// 返回上一个ViewController对象

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController{

NSUInteger index = [self indexOfViewController:(MoreViewController *)viewController];
if ((index == 0) || (index == NSNotFound)) {
return nil;
}

index--;

// 返回的ViewController,将被添加到相应的UIPageViewController对象上。

// UIPageViewController对象会根据UIPageViewControllerDataSource协议方法,自动来维护次序。

// 不用我们去操心每个ViewController的顺序问题。

return [self viewControllerAtIndex:index];

}

// 返回下一个ViewController对象

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController{

NSUInteger index = [self indexOfViewController:(MoreViewController *)viewController];
if (index == NSNotFound) {
return nil;
}
index++;
/**
*  如果原来加载的数据加载完毕了,就加载更多数据
*/
if (index == loadMaxPage * self.loadTimes) {
[self loadMoreData];
}
if (index == [self.pageContent count]) {
return nil;
}
return [self viewControllerAtIndex:index];
}

/**
 *  加载更多数据
 */
- (void)loadMoreData
{
    /**
     *  loadMaxPage
     *
     *  保存用户初始化加载最大的页数
     */
    int nowPage = loadMaxPage * self.loadTimes ;
    int loadPage = loadMaxPage+1;
    for (int i = 0; i<loadPage; i++) {
        PDFView *paf =[[PDFView alloc]initWithFrame:self.view.frame atPage:nowPage + i];
        [self.pageContent addObject:paf];
    }
    
    (self.loadTimes ++);
    
}




这里做了一个小处理,如果pdf文件页数太多,就加载一部分页面,等到用户翻到最后一张,就再次加载一部分数据
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: