Convert PDF data to UIImage and download into album in Swift iOS

I have got a requirement like from REST API, I will get the PDF bytes data, So, In UI I have to convert the PDF bytes data to image(.jpg) format and download into photo Album. Is it Possible in Swift4? if Possible, can share me an example code snippet.

Thanks in advance

Upvotes: 2

Views: 1627

Answers (3)

This code is working fine for my question:

    GHServiceManager.shared.getPDF(fileName: self.pdfName, success: { (ssdata) in

                                                   let pdfData = ssdata as CFData
                                                   let provider:CGDataProvider = CGDataProvider(data: pdfData)!
                                                   let pdfDoc:CGPDFDocument = CGPDFDocument(provider)!
                                                   let pdfPage:CGPDFPage = pdfDoc.page(at: 1)!
                                                   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()

                                                    dukePhotoLibrary?.saveImage(image:  pdfImage)
})

Upvotes: 2

Bhavesh.iosDev
Bhavesh.iosDev

Reputation: 942

you can save your pdf to document directory and can create thumbnail of first page

import PDFKit
func pdfThumbnail(url: URL, width: CGFloat = 240) -> UIImage? {
            guard let data = try? Data(contentsOf: url),
                let page = PDFDocument(data: data)?.page(at: 0) else {
                    return nil
            }

            let pageSize = page.bounds(for: .mediaBox)
            let pdfScale = width / pageSize.width

            // Apply if you're displaying the thumbnail on screen
            let scale = UIScreen.main.scale * pdfScale
            let screenSize = CGSize(width: pageSize.width * scale,
                                    height: pageSize.height * scale)

            return page.thumbnail(of: screenSize, for: .mediaBox)
        }

Upvotes: 4

flanker
flanker

Reputation: 4200

Check out PDFKit in the documentation.

You can initialise a PDF document with a Data representation of a PDF (or a PDF file) and then display it in a PDFView. Given PDFView inherits from UIView, all the standard UIView functionality should be there, including methods such as

func UIImageWriteToSavedPhotosAlbum(UIImage, Any?, Selector?, UnsafeMutableRawPointer?)

which should do what it says in it's signature!

Upvotes: 2

Related Questions