将多页PDF文件转换为多张JPG图片

来源:互联网 发布:淘宝代运营托管 编辑:程序博客网 时间:2024/05/16 19:38
一开始以为做不到,不过iOS原生的库还是挺强大的。 

直接上Code吧 


Obj-c代码  收藏代码
  1. -(void) createJPGsFromPDF:(NSString *)fromPDFName  
  2. {  
  3.       
  4.     if (fromPDFName == nil || [fromPDFName isEqualToString:@""]) {  
  5.         return;  
  6.     }  
  7.       
  8.     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  9.     NSString *documentsDir = [paths objectAtIndex:0];  
  10.       
  11.     NSString *docPath = [documentsDir stringByAppendingPathComponent:fromPDFName];  
  12.     NSURL *fromPDFURL = [NSURL fileURLWithPath:docPath];  
  13.     CGPDFDocumentRef fromPDFDoc = CGPDFDocumentCreateWithURL((CFURLRef) fromPDFURL);  
  14.       
  15.     // Get Total Pages  
  16.     int pages = CGPDFDocumentGetNumberOfPages(fromPDFDoc);  
  17.       
  18.     // Create Folder for store under "Documents/"  
  19.     NSError *error = nil;  
  20.     NSFileManager *fileManager = [[NSFileManager alloc] init];  
  21.     NSString *folderPath = [documentsDir stringByAppendingPathComponent:[fromPDFName stringByDeletingPathExtension]];  
  22.     [fileManager createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:&error];  
  23.     [fileManager release];  
  24.       
  25.     int i = 1;  
  26.     for (i = 1; i <= pages; i++) {  
  27.         CGPDFPageRef pageRef = CGPDFDocumentGetPage(fromPDFDoc, i);  
  28.         CGPDFPageRetain(pageRef);  
  29.           
  30.         // determine the size of the PDF page  
  31.         CGRect pageRect = CGPDFPageGetBoxRect(pageRef, kCGPDFMediaBox);  
  32.           
  33.         // renders its content.  
  34.         UIGraphicsBeginImageContext(pageRect.size);  
  35.           
  36.         CGContextRef imgContext = UIGraphicsGetCurrentContext();  
  37.         CGContextSaveGState(imgContext);  
  38.         CGContextTranslateCTM(imgContext, 0.0, pageRect.size.height);  
  39.         CGContextScaleCTM(imgContext, 1.0, -1.0);  
  40.         CGContextSetInterpolationQuality(imgContext, kCGInterpolationDefault);  
  41.         CGContextSetRenderingIntent(imgContext, kCGRenderingIntentDefault);  
  42.         CGContextDrawPDFPage(imgContext, pageRef);  
  43.         CGContextRestoreGState(imgContext);  
  44.           
  45.         //PDF Page to image  
  46.         UIImage *tempImage = UIGraphicsGetImageFromCurrentImageContext();  
  47.           
  48.         UIGraphicsEndImageContext();  
  49.         //Release current source page  
  50.         CGPDFPageRelease(pageRef);  
  51.           
  52.         // Store IMG  
  53.         NSString *imgname = [NSString stringWithFormat:@"fromPDFName_%d.jpg", i];  
  54.         NSString *imgPath = [folderPath stringByAppendingPathComponent:imgname];  
  55.         [UIImageJPEGRepresentation(tempImage, 1.0) writeToFile:imgPath atomically:YES];  
  56.           
  57.     }  
  58.       
  59.     CGPDFDocumentRelease(fromPDFDoc);  
  60.       
转载自http://woniu1983.iteye.com/blog/1767008

0 0