iOS PDF 及 缩略图

来源:互联网 发布:国内三大耳机论坛 知乎 编辑:程序博客网 时间:2024/05/19 00:38

上回说到这个FastPDF还是不错的,至少用于demo学习,足以。下载:Download now FastPdfKit

FastPdfKit is available in 4 versions: Free, Basic, Plus and Extra, all availables throgh the public repository on github. 目前来说有4个版本,均可以在github得到下载

In the repository you'll find a sample Xcode project with many targets that you can customize based on your needs. To start you can try the static version distributed as framework (click here for project direct download) that will let you add pdf support in your applications with a single step. (通过添加静态库,然后使用的方式就可以通过简单的步骤让你的APP支持PDF,当然不是仅仅浏览了)

With the free version you can use every FastPdfKit features for an unlimited period. The single limitation is a Splash Screen with FastPdfKit logo when you open every pubblication.You can obviously use this version to distribute applications on the App Store, no limits. 使用免费版本,你能够得到所有FastPDFkit的特性,只不过需要看到一个弹出界面,同时你还可以使用free的版本,单发布到App store,这个是完全可以的。

http://fastpdfkit.com/ios-framework:

Precise zoom on annotations and page parts 这个算个feature,可以在批注上zoom//

Auto mode: single page in portrait and double page in landscape: 这个就自动布局,如果是landscape就双栏显示

Added a method to retrieve annotations from the document and provide them as overlays, 在2011的某个版本中,增加了获取批注并以overlay的方式呈现的功能:


在下载的代码中:

[cpp] view plaincopy
  1. MenuViewController *aMenuViewController = nil;  
  2.   
  3. f(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {  
  4.     aMenuViewController = [[MenuViewController alloc]initWithNibName:@"MenuView_pad" bundle:[NSBundle mainBundle]];  
  5.  else {  
  6.     aMenuViewController = [[MenuViewController alloc]initWithNibName:@"MenuView_phone" bundle:[NSBundle mainBundle]];  


 要实现下面截图的功能,啥也不用干,采用一个readerview,再加一个documentmanager


 


基本步骤3steps:

1. 把下载得到的所有library和build信息,xcconfig,整个目录copy到你自己的项目中

2. 修改project configuration info,选择fastodfkitframework,

3,完成代码,调用vc,打开PDF文件

    /** Present the pdf on screen in a modal view */

    [selfpresentModalViewController:pdfViewControlleranimated:YES]; 


ReaderVC->MFDocumentmanger->NSURL->DocumentName:

会自动把缩略图生成到目录下,且后续能够点击缩略图后关联navigate到特定page

 /** Set resources folder on the manager */

    documentManager.resourceFolder = thumbnailsPath;

==============

NSFileManager *manager = [NSFileManagerdefaultManager];

    NSDirectoryEnumerator *direnum = [manager enumeratorAtPath:thumbnailsPath];

    

    NSString *filename;

    

    while ((filename = [direnum nextObject] )) {

        

        NSLog(@"%@",filename);

                

    }



=========

关于PDF缩略图,方法一直接从PDF文件中获取,方法二,自己利用一些方法产生(例如using quartz) ,然后保存下来:方法二:

- (UIImage *)imageFromPDFWithDocumentRef:(CGPDFDocumentRef)documentRef {    CGPDFPageRef pageRef = CGPDFDocumentGetPage(documentRef, 1);    CGRect pageRect = CGPDFPageGetBoxRect(pageRef, kCGPDFCropBox);    UIGraphicsBeginImageContext(pageRect.size);    CGContextRef context = UIGraphicsGetCurrentContext();    CGContextTranslateCTM(context, CGRectGetMinX(pageRect),CGRectGetMaxY(pageRect));    CGContextScaleCTM(context, 1, -1);      CGContextTranslateCTM(context, -(pageRect.origin.x), -(pageRect.origin.y));    CGContextDrawPDFPage(context, pageRef);    UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();    return finalImage;} 

Change the above function to this: <code> - (UIImage *)imageFromPDFWithDocumentRef:(CGPDFDocumentRef)documentRef and PageNumber:(int)pageNumber { CGPDFPageRef pageRef = CGPDFDocumentGetPage(documentRef, pageNumber);</code> then call it in a loop.

看另外一个代码,通过新起thread来处理,并且cache image:

However If you are targeting iOS 4 or above I would strongly recommend using blocks. (Also graphics contexts are thread safe since 4).

I found a significant performance increase when I generated thumbs with blocks.

What I have done in the past is:

  • Have a ViewController for your thumbs, it has a scrollview that has a content size appropriate for all your pages. Insert Placeholder ImageViews into is if you like.

  • On document load, kick off a thumb generator in the background (see code below)

  • The code below calls a method drawImageView that takes the index of the page, grabs the image from the disk and puts it into the scroll view

  • If your feeling really motivated you can implement a render scope on the thumb scrollView (only rendering the thumbs you need to - something you should be doing for the pdf's anyway)

  • Dont forget to delete the thumbs when your done, unless you want to cache..

#define THUMB_SIZE 100,144

-(void)generateThumbsWithGCD{    thumbQueue = dispatch_queue_create("thumbQueue", 0);//thumbQueue = dispatch_queue_t    NSFileManager *fm = [NSFileManager defaultManager];    //good idea to check for previous thumb cache with NSFileManager here       CGSize thumbSize = CGSizeMake(THUMB_SIZE);    __block CGPDFPageRef myPageRef;         NSString *reqSysVer = @"4.0";       NSString *currSysVer = [[UIDevice currentDevice] systemVersion];        //need to handle ios versions < 4    if ([currSysVer compare:reqSysVer options:NSNumericSearch] == NSOrderedAscending) {NSLog(@"UIKIT MULTITHREADING NOT SUPPORTED!");return;}//thread/api safteydispatch_async(thumbQueue, ^{    for (i=1; i<=_maxPages; i++) {              //check if worker is valid (class member bool) for cancelations        myPageRef=[[PDFDocument sharedPDFDocument]getPageData:i];//pdfdocument is a singleton class             if(!myPageRef)return;            NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];            NSString* imageName = [NSString stringWithFormat:@"%@thumb%i.png",documentName,i];              NSString* fullPathToFile =  [thumbDocPath stringByAppendingPathComponent:imageName];            if(![fm fileExistsAtPath:fullPathToFile]){                //NSLog(@"Not there");                UIGraphicsBeginImageContext(thumbSize);//thread Safe in iOs4                CGContextRef context = UIGraphicsGetCurrentContext();//thread Safe in iOs4                          CGContextTranslateCTM(context, 0, 144);                CGContextScaleCTM(context, 0.15, -0.15);                CGContextDrawPDFPage (context, myPageRef);                UIImage * render = UIGraphicsGetImageFromCurrentImageContext();                                     UIGraphicsEndImageContext();                        NSData* imageData= UIImagePNGRepresentation(render);                if(imageData){                    NSLog(@"WROTE TO:%@",fullPathToFile);                    if(![imageData writeToFile:fullPathToFile atomically:NO])NSLog(@"ERROR: Thumb Didnt Save"); //COMMENT OUT TO DISABLE WRITE                                  }                           }            else NSLog(@"Allready There! %@",fullPathToFile);                    //update progress on thumb viewController if you wish here            [pool release];            dispatch_sync(dispatch_get_main_queue(), ^{            [self drawImageView:i];        });    }       });    dispatch_release(thumbQueue);}

If I needed to blow away the doc then on a level 2 warning I do simply that but also set a flag that my background tasks check periodically. Your thumbnail generator needs to have logic in it (ie at the start of the loop or at least before it accesses the doc) that can end it generation prematurely if it sees this flag. If before you start a thumb you also check for if it already exists (or do a full check first) then it will not have to redo the thumbs already cached (this is also valid for reopening documents and changing the doc ref half way through thumb operations too)

===原始的方式增加annotation
- (void) exampleAnnotation;{    NSURL* url = [NSURL fileURLWithPath:[self getPDFFileName]];    CGPDFDocumentRef document = CGPDFDocumentCreateWithURL ((CFURLRef) url);// 2    size_t count = CGPDFDocumentGetNumberOfPages (document);// 3    if (count == 0)    {        NSLog(@"PDF needs at least one page");        return;    }    CGRect paperSize = CGRectMake(0.0,0.0,595.28,841.89);    UIGraphicsBeginPDFContextToFile([self getTempPDFFileName], paperSize, nil);    UIGraphicsBeginPDFPageWithInfo(paperSize, nil);    CGContextRef currentContext = UIGraphicsGetCurrentContext();    // flip context so page is right way up    CGContextTranslateCTM(currentContext, 0, paperSize.size.height);    CGContextScaleCTM(currentContext, 1.0, -1.0);     CGPDFPageRef page = CGPDFDocumentGetPage (document, 1); // grab page 1 of the PDF     CGContextDrawPDFPage (currentContext, page); // draw page 1 into graphics context  ---PDF内容     // flip context so annotations are right way up    CGContextScaleCTM(currentContext, 1.0, -1.0);    CGContextTranslateCTM(currentContext, 0, -paperSize.size.height);    [@"Example annotation" drawInRect:CGRectMake(100.0, 100.0, 200.0, 40.0) withFont:[UIFont systemFontOfSize:18.0]];    UIGraphicsEndPDFContext();---- 完成PDFcontext    CGPDFDocumentRelease (document);}//这样的,应该无法提供查找功能的

===========About XCconfig

I just switched over to Xcode 4 and I'm having trouble working with .xcconfig files for build settings. My targets that already had files assigned in Xcode 3 are all set up - a column titled Config.file shows up in the Levels view of Build Settings. But I can't figure out how to edit the columns in this view or where to put an .xcconfig file to attach it to a new target. I've looked online and haven't found an answer. Anybody know how to do this?

===

Pick File | New, choose Other and "Configuration Settings File", this gets you a new .xcconfig file.

Then click on the project on the upper left in the file browser (the same place where you get to build settings), click on the project again in the second-to-left panel, and click on the Info tab at the top of the inner panel.

There, you can choose "Based on Configuration File" for each of your targets for each of your configurations (debug, release, etc.)


http://blog.csdn.net/gnicky/article/details/7491902#comments

原创粉丝点击