简体   繁体   English

将 PDF 转换为 UIImage

[英]Convert PDF to UIImage

func drawOnPDF(path: String)
{
    // Get existing Pdf reference
    let pdf = CGPDFDocumentCreateWithURL(NSURL(fileURLWithPath: path))

    // Get page count of pdf, so we can loop through pages and draw them accordingly
    let pageCount = CGPDFDocumentGetNumberOfPages(pdf);

    // Write to file
    UIGraphicsBeginPDFContextToFile(path, CGRectZero, nil)

    // Write to data
//        var data = NSMutableData()
//        UIGraphicsBeginPDFContextToData(data, CGRectZero, nil)

    for index in 1...pageCount {
        let page = CGPDFDocumentGetPage(pdf, index)
        let pageFrame = CGPDFPageGetBoxRect(page, kCGPDFMediaBox)

        UIGraphicsBeginPDFPageWithInfo(pageFrame, nil)

        var ctx = UIGraphicsGetCurrentContext()

        // Draw existing page
        CGContextSaveGState(ctx);
        CGContextScaleCTM(ctx, 1, -1);
        CGContextTranslateCTM(ctx, 0, -pageFrame.size.height);
        CGContextDrawPDFPage(ctx, page);
        CGContextRestoreGState(ctx);

        // Draw image on top of page
        var image = UIImage(named: "signature3")
        image?.drawInRect(CGRectMake(100, 100, 100, 100))
        // Draw red box on top of page
        //UIColor.redColor().set()
        //UIRectFill(CGRectMake(20, 20, 100, 100));
    }

    UIGraphicsEndPDFContext()
}

My problem is PDF convert to image but how to open image in View then swipe left,right,up and down how to possible我的问题是 PDF 转换为图像但是如何在视图中打开图像然后向左、向右、向上和向下滑动怎么可能

Swift 3:斯威夫特 3:

func convertPDFPageToImage(page:Int) {

    let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
    let filePath = documentsURL.appendingPathComponent("pathLocation").path

    do {

        let pdfdata = try NSData(contentsOfFile: filePath, options: NSData.ReadingOptions.init(rawValue: 0))

        let pdfData = pdfdata as CFData
        let provider:CGDataProvider = CGDataProvider(data: pdfData)!
        let pdfDoc:CGPDFDocument = CGPDFDocument(provider)!
        pageCount = pdfDoc.numberOfPages;
        let pdfPage:CGPDFPage = pdfDoc.page(at: page)!
        var pageRect:CGRect = pdfPage.getBoxRect(.mediaBox)
        pageRect.size = CGSize(width:pageRect.size.width, height:pageRect.size.height)

        print("\(pageRect.width) by \(pageRect.height)")

        UIGraphicsBeginImageContext(pageRect.size)
        let context:CGContext = UIGraphicsGetCurrentContext()!
        context.saveGState()
        context.translateBy(x: 0.0, y: pageRect.size.height)
        context.scaleBy(x: 1.0, y: -1.0)
        context.concatenate(pdfPage.getDrawingTransform(.mediaBox, rect: pageRect, rotate: 0, preserveAspectRatio: true))
        context.drawPDFPage(pdfPage)
        context.restoreGState()
        let pdfImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()            

        self.imageView.image = pdfImage

    }
    catch {

    }

}

I Got the Solution for Pdf page convert in UIImage.我得到了 UIImage 中 Pdf 页面转换的解决方案。 This code PDF Page convert in Uiimage and save in image document directory.此代码 PDF 页面在 Uiimage 中转换并保存在图像文档目录中。 PDF Download And Page Count to separate Page vise image convert. PDF 下载和页面计数以分离页面虎钳图像转换。

    func drawOnPDF(path: String)
    {
        var urlstr: NSURL = NSURL.fileURLWithPath(path)!
        var pdf: CGPDFDocumentRef = CGPDFDocumentCreateWithURL(urlstr)
        var page: CGPDFPageRef;
        var frame: CGRect = CGRectMake(0, 0, 100, 200)
        var pageCount: Int = CGPDFDocumentGetNumberOfPages(pdf);
        for (var i = 0; i < pageCount; i++)
        {
            var mypage: CGPDFPageRef = CGPDFDocumentGetPage(pdf, i+1)
            frame = CGPDFPageGetBoxRect(mypage, kCGPDFMediaBox)
            UIGraphicsBeginImageContext(CGSizeMake(600, 600*(frame.size.height/frame.size.width)))
            var ctx: CGContextRef = UIGraphicsGetCurrentContext()
            CGContextSaveGState(ctx)
            CGContextTranslateCTM(ctx, 0.0, frame.size.height)
            CGContextScaleCTM(ctx, 1.0, -1.0)
            CGContextSetGrayFillColor(ctx, 1.0, 1.0)
            CGContextFillRect(ctx, frame)
            page = CGPDFDocumentGetPage(pdf, i + 1)
            var pdfTransform: CGAffineTransform = CGPDFPageGetDrawingTransform(page, kCGPDFMediaBox, frame, 0, true)
            CGContextConcatCTM(ctx, pdfTransform);
            CGContextSetInterpolationQuality(ctx, kCGInterpolationHigh)
            CGContextSetRenderingIntent(ctx, kCGRenderingIntentDefault)
            CGContextDrawPDFPage(ctx, page)
            var thumbnailImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
            CGContextRestoreGState(ctx)
            var documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as! String 
            documentsPath = documentsPath.stringByAppendingFormat("/Page%d.png", i+1)
            UIGraphicsEndImageContext()
            imagedata = UIImagePNGRepresentation(thumbnailImage)
            imagedata.writeToFile(documentsPath, atomically: true)
            array.addObject(documentsPath)
        }
        let dirPath = array.objectAtIndex(0) as? String
        let image    = UIImage(contentsOfFile: dirPath!)
        NSUserDefaults.standardUserDefaults().setObject(array, forKey: "Array")
        println("\(array)")
    }

I have found a cool solution.我找到了一个很酷的解决方案。 Borrowed from here: https://www.hackingwithswift.com/example-code/core-graphics/how-to-render-a-pdf-to-an-image从这里借用: https : //www.hackingwithswift.com/example-code/core-graphics/how-to-render-a-pdf-to-an-image

func drawPDFfromURL(url: URL) -> UIImage? {
    guard let document = CGPDFDocument(url as CFURL) else { return nil }
    guard let page = document.page(at: 1) else { return nil }

    let pageRect = page.getBoxRect(.mediaBox)
    let renderer = UIGraphicsImageRenderer(size: pageRect.size)
    let img = renderer.image { ctx in
        UIColor.white.set()
        ctx.fill(pageRect)

        ctx.cgContext.translateBy(x: 0.0, y: pageRect.size.height)
        ctx.cgContext.scaleBy(x: 1.0, y: -1.0)

        ctx.cgContext.drawPDFPage(page)
    }

    return img
}

All these solution converts a single pdf page into an UIImage .所有这些解决方案将单个 pdf 页面转换为UIImage Here is my findings for converting multiple page pdf into a single image这是我将多页 pdf 转换为单个图像的发现

func drawPDFfromURL(url: URL) -> UIImage? {
        guard let document = CGPDFDocument(url as CFURL) else { return nil }

        var width: CGFloat = 0
        var height: CGFloat = 0
        
        // calculating overall page size
        for index in 1...document.numberOfPages {
            print("index: \(index)")
            if let page = document.page(at: index) {
                let pageRect = page.getBoxRect(.mediaBox)
                width = max(width, pageRect.width)
                height = height + pageRect.height
            }
        }

        // now creating the image
        let renderer = UIGraphicsImageRenderer(size: CGSize(width: width, height: height))

        let image = renderer.image { (ctx) in
            ctx.cgContext.scaleBy(x: 1.0, y: -1.0)
            for index in 1...document.numberOfPages {
                
                if let page = document.page(at: index) {
                    let pageRect = page.getBoxRect(.mediaBox)
                    ctx.cgContext.translateBy(x: 0.0, y: -pageRect.height)
                    ctx.cgContext.drawPDFPage(page)
                }
            }
            
            ctx.cgContext.scaleBy(x: 1.0, y: -1.0)
        }
        return image
    }

Using PDFKit:使用 PDFKit:

1)Get the pdf path 1)获取pdf路径

2)Access the pdfpage which you want to convert to image: 2)访问要转换为图像的pdf页面:

  let pdfPage = PDFDocument(url: pdfPath)?.page(at: 0)

3)Convert the page tp image using thumbnail function: 3)使用缩略图function转换页面tp图像:

   let pdfImage = pdfPage?.thumbnail(of: self.pdfPageBounds.size, for: .mediaBox)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM