美文网首页
Quartz2d--pdf转图片

Quartz2d--pdf转图片

作者: i诺离 | 来源:发表于2017-01-19 16:54 被阅读84次
    -(void) createJPGsFromPDF:(NSString *)fromPDFName
    {
        
        if (fromPDFName == nil || [fromPDFName isEqualToString:@""]) {
            return;
        }
        
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDir = [paths objectAtIndex:0];
        
        NSString *docPath = [documentsDir stringByAppendingPathComponent:fromPDFName];
            
        NSURL *fromPDFURL = [NSURL fileURLWithPath:docPath];
        CGPDFDocumentRef fromPDFDoc = CGPDFDocumentCreateWithURL((CFURLRef) fromPDFURL);
        
        // Get Total Pages
        unsigned long pages = CGPDFDocumentGetNumberOfPages(fromPDFDoc);
        
        // Create Folder for store under "Documents/"
        NSError *error = nil;
        NSFileManager *fileManager = [[NSFileManager alloc] init];
        NSString *folderPath = [documentsDir stringByAppendingPathComponent:[fromPDFName stringByDeletingPathExtension]];
        [fileManager createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:&error];
        
        int i = 1;
        for (i = 1; i <= pages; i++) {
            CGPDFPageRef pageRef = CGPDFDocumentGetPage(fromPDFDoc, i);
            CGPDFPageRetain(pageRef);
            
            // determine the size of the PDF page
            CGRect pageRect = CGPDFPageGetBoxRect(pageRef, kCGPDFMediaBox);
            
            // renders its content.
            UIGraphicsBeginImageContext(pageRect.size);
    
            
            CGContextRef imgContext = UIGraphicsGetCurrentContext();
            CGContextSaveGState(imgContext);
            
            CGContextTranslateCTM(imgContext, 0, pageRect.size.height);
            CGContextScaleCTM(imgContext, 1, -1);
            
            // 调图片质量
            CGContextSetInterpolationQuality(imgContext, kCGInterpolationHigh);
            // 渲染意图
            CGContextSetRenderingIntent(imgContext, kCGRenderingIntentDefault);
            
            // 将pdf画道image上
            CGContextDrawPDFPage(imgContext, pageRef);
            CGContextRestoreGState(imgContext);
            
            //PDF Page to image
            UIImage *tempImage = UIGraphicsGetImageFromCurrentImageContext();
            
            UIGraphicsEndImageContext();
            //Release current source page
            CGPDFPageRelease(pageRef);
            
            // Store IMG
            NSString *imgname = [NSString stringWithFormat:@"fromPDFName_%d.jpg", i];
            NSString *imgPath = [folderPath stringByAppendingPathComponent:imgname];
            // UIImageJPEGRepresentation(tempImage, 0.1) 图片转为NSData并压缩大小
            [UIImageJPEGRepresentation(tempImage, 0.1) writeToFile:imgPath atomically:YES];
    
            
        }
        
        
        CGPDFDocumentRelease(fromPDFDoc);
        
    }
    

    相关文章

      网友评论

          本文标题:Quartz2d--pdf转图片

          本文链接:https://www.haomeiwen.com/subject/lzpibttx.html