国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS下PDF文件的瀏覽和涂鴉效果的簡單實現

2020-07-26 03:32:57
字體:
來源:轉載
供稿:網友

瀏覽PDF的效果
方法一:利用webview

復制代碼 代碼如下:

-(void)loadDocument:(NSString *)documentName inView:(UIWebView *)webView 

    NSString *path = [[NSBundle mainBundle] pathForResource:documentName ofType:nil]; 
    NSURL *url = [NSURL fileURLWithPath:path]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    [webView loadRequest:request]; 


利:1.實現簡單
        2.還是實現簡單
弊:1.僅能瀏覽,拿不到任何回調,safari不會鳥任何人。
        2.固定豎版拖動,想實現翻頁動效果就扒瞎

2015102894335125.png (424×555)

下面的方法可以解決webview 顯示pdf的弊,相對的,要付出一些汗水作為代價了。

方法二:利用CGContextDrawPDFPage

復制代碼 代碼如下:

CGPDFDocumentRef GetPDFDocumentRef(NSString *filename) 

    CFStringRef path; 
    CFURLRef url; 
    CGPDFDocumentRef document; 
    size_t count; 
     
    path = CFStringCreateWithCString (NULL, [filename UTF8String], kCFStringEncodingUTF8); 
    url = CFURLCreateWithFileSystemPath (NULL, path, kCFURLPOSIXPathStyle, 0); 
     
    CFRelease (path); 
    document = CGPDFDocumentCreateWithURL (url); 
    CFRelease(url); 
    count = CGPDFDocumentGetNumberOfPages (document); 
    if (count == 0) { 
        printf("[%s] needs at least one page!/n", [filename UTF8String]); 
        return NULL;  
    } else { 
        printf("[%ld] pages loaded in this PDF!/n", count); 
    } 
    return document; 

 
void DisplayPDFPage (CGContextRef myContext, size_t pageNumber, NSString *filename) 

    CGPDFDocumentRef document; 
    CGPDFPageRef page; 
     
    document = GetPDFDocumentRef (filename); 
    page = CGPDFDocumentGetPage (document, pageNumber); 
    CGContextDrawPDFPage (myContext, page); 
    CGPDFDocumentRelease (document); 


這樣顯示出來的pdf單頁是倒立的,Quartz坐標系和UIView坐標系不一樣所致,調整坐標系,使pdf正立:

復制代碼 代碼如下:

CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextTranslateCTM(context, 80, self.frame.size.height-60); 
CGContextScaleCTM(context, 1, -1); 


配合iOS5強大的UIPageViewController實現翻頁瀏覽

復制代碼 代碼如下:

- (PDFViewController *)viewControllerAtIndex:(NSUInteger)index  

    //Return the PDFViewController for the given index. 
    if (([self.pagePDF count] == 0 )|| (index > [self.pagePDF count]) ) { 
        return nil; 
    } 
     
    //Create a new view controller and pass suitable data. 
    PDFViewController *dataViewController = [[PDFViewController alloc]initWithNibName:@"PDFViewController" bundle:nil]; 
    //dataViewController.pdfview = [self.pagePDF objectAtIndex:index]; 
    dataViewController.pdfview = [[PDFView alloc]initWithFrame:self.view.frame atPage:index]; 
    [dataViewController.view addSubview:dataViewController.pdfview]; 
    NSLog(@"index = %d",index); 
    return dataViewController; 

 
- (NSUInteger) indexOfViewController:(PDFViewController *)viewController 

    return [self.pagePDF indexOfObject:viewController.pdfview]; 

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

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

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

    NSUInteger index = [self indexOfViewController:(PDFViewController *)viewController]; 
    if (index == NSNotFound) 
    { 
        return nil; 
    } 
     
    index++; 
     
    if (index == [self.pagePDF count]){ 
        return  nil; 
    } 
     
    return [self viewControllerAtIndex:index]; 


2015102894406303.png (385×514)

2015102894427910.png (385×512)

涂鴉效果
主要涉及:
1. 多context,分層畫畫

復制代碼 代碼如下:

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx


2. 觸摸事件touches族那些event

復制代碼 代碼如下:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
……


3. 初始化單頁view傳頁碼

復制代碼 代碼如下:

- (id)initWithFrame:(CGRect)frame onPage:(NSInteger)page


4.畫軌跡方法

復制代碼 代碼如下:

CG_EXTERN void CGPathMoveToPoint(CGMutablePathRef path,
  const CGAffineTransform *m, CGFloat x, CGFloat y)
CG_EXTERN void CGPathAddLineToPoint(CGMutablePathRef path,
  const CGAffineTransform *m, CGFloat x, CGFloat y)


2015102894449261.png (383×512)

2015102894506965.png (386×513)

2015102894558662.png (770×575)

2015102894619851.png (770×577)

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 缙云县| 特克斯县| 重庆市| 会泽县| 新乐市| 保康县| 盐源县| 长沙县| 康定县| 玛曲县| 绥江县| 禄劝| 哈巴河县| 温宿县| 扎囊县| 类乌齐县| 金门县| 大兴区| 环江| 万载县| 西城区| 天门市| 永登县| 舒兰市| 桂阳县| 屯门区| 永吉县| 定兴县| 绥化市| 岳阳市| 巴塘县| 德江县| 沁源县| 包头市| 丰镇市| 赤壁市| 呼图壁县| 依安县| 平罗县| 阳春市| 灵川县|