关于UIWebView 加载网络PDF 实现翻页效果的那些事

首先呢  这里说的是加载网络的pdf  可不是本地的哦  

新建一个view  主要是用于循环渲染绘制pdf单页面内容

.h重写init方法 用于自定义 接收docRef文件和传入的page页面

- (instancetype)initWithFrame:(CGRect)frame documentRef:(CGPDFDocumentRef)docRef andPageNum:(int)page;

.m页面呢 实现方法  并且需要属性方法

CGPDFDocumentRef  documentRef;  这个是pdf接收的doc文件类型

NSInteger pageNum;    接收当前的page

- (instancetype)initWithFrame:(CGRect)frame documentRef:(CGPDFDocumentRef)docRef andPageNum:(int)page{

    self = [super initWithFrame:frame];

    if(self){

        documentRef= docRef;

       _pageNum= page;

        self.backgroundColor= [UIColor whiteColor];

    }

    return self;

}

然后呢 因为是循环渲染绘制  需要重写 drawRect  重写此方法,执行重绘任务

drawRect调用是在Controller->loadView,,Controller->viewDidLoad 两方法之后调用的

- (void)drawRect:(CGRect)rect {

    [self drawPDFIncontext:UIGraphicsGetCurrentContext()];//将当前的上下文环境传递  新建方法

}

好了  最重要的一部分要来了

- (void)drawPDFIncontext:(CGContextRef)context {

    //调整图形的位置 

//Quartz坐标系和UIView坐标系不一样所致,调整坐标系,使pdf正立

    CGContextTranslateCTM(context,0.0,self.frame.size.height);

    //图形呈正立显示

    CGContextScaleCTM(context,1.0, -1.0);

/获取指定页的pdf文档

    CGPDFPageRef  pageRef =CGPDFDocumentGetPage(documentRef,_pageNum);

//记录当前绘制环境,防止多次绘画

    CGContextSaveGState(context);

//创建一个仿射变换,该变换基于将PDF页的BOX映射到指定的矩形中。

    CGAffineTransform  pdfTransForm =CGPDFPageGetDrawingTransform(pageRef,kCGPDFCropBox,self.bounds,0,true);

    CGContextConcatCTM(context, pdfTransForm);

//将pdf绘制到上下文中

    CGContextDrawPDFPage(context, pageRef);

//将pdf保存

    CGContextRestoreGState(context);

}

 

好了 自定义的view 实现的方法就是这些了  然后回到vc页面去

主要是在.m里面的  viewDidLoad

下面 我要开始了哦

翻页我们可以想到很多 比如UIPageViewController  UICollectionView等  这里我主要说的是UIPageViewController

@property (nonatomic,strong) UIPageViewController *pageVC;

@property (nonatomic,strong) NSMutableArray *pdfArr;

这个你们都懂吧

  self.pdfArr = [NSMutableArray array];

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

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

    _pageVC.view.frame = self.view.bounds;

    _pageVC.delegate = self;

    _pageVC.dataSource = self;

    [self addChildViewController:_pageVC];

    

    CGPDFDocumentRef pdfRef = [self pdfRefByDataByUrl:@"http://eb18036.ebenny.com/new_huasun_server/upload//pdf//1527047215118.pdf"];

    size_t count = CGPDFDocumentGetNumberOfPages(pdfRef);//这个位置主要是获取pdf页码数;

    

    for (int i = 0; i < count; i++) {

        UIViewController *pdfVC = [[UIViewController alloc] init];

        LLPDFView *pdfView = [[LLPDFView alloc]initWithFrame:self.view.bounds documentRef:pdfRef andPageNum:i+1];

        

        [pdfVC.view addSubview:pdfView];

        

        [_pdfArr addObject:pdfVC];

    }

    

    

    [_pageVC setViewControllers:[NSArray arrayWithObject:_pdfArr[0]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];

    [self.view addSubview:_pageVC.view];

 

 

- (CGPDFDocumentRef)pdfRefByDataByUrl:(NSString *)aFileUrl {

    NSURL*url = [NSURL URLWithString:aFileUrl];

    CFURLRef refURL = (__bridge_retained CFURLRef)url;

    CGPDFDocumentRef document =CGPDFDocumentCreateWithURL(refURL);

    if(refURL){

        CFRelease(refURL);

    }

    if(document) {

        

        return  document;//返回获取到的数据

        

    }else{

        

        return   NULL; //如果没获取到数据,则返回NULL,当然,你可以在这里添加一些打印日志,方便你发现问题

        

    }

    

}

 

- (UIViewController *)viewControllerAtIndex:(NSInteger)index

{

    //Create a new view controller and pass suitable data.

    

    if (([_pdfArr count] == 0 )|| (index > [_pdfArr count]) ) {

        return nil;

    }

    

    

    NSLog(@"index = %ld",(long)index);

    

    return (UIViewController *)_pdfArr[index];

}

 

 

- (NSUInteger) indexOfViewController:(UIViewController *)viewController

{

    return [self.pdfArr indexOfObject:viewController];

}

 

 

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

{

    NSUInteger index = [self indexOfViewController:(UIViewController *)viewController];

    if (index == NSNotFound) {

        return nil;

    }

    

    index++;

    

    if (index == [_pdfArr count]){

        return  nil;

    }

    

    return [self viewControllerAtIndex:index];

}

 

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

{

    NSUInteger index = [self indexOfViewController:(UIViewController *)viewController];

    if ((index == 0 ) || (index == NSNotFound)){

        return nil;

    }

    

    index--;

    

    return [self viewControllerAtIndex:index];

}

posted on 2018-05-24 13:55  廖利君  阅读(242)  评论(0编辑  收藏  举报