Swift How to Compress PDF Size by CGContext , I am Converted image to PDF , But PDF size is to Big

Viewed 19

I am converting Image to PDF and save in the documentation directory. Image size is 1 MB and PDF size 10MB. This is very difficult to share any one. Need your help to compress pdf file when I write to pdf file in the document directory. Here is my CODE

func createPDF(image: UIImage) -> NSData? {
           let pdfData = NSMutableData()
           let pdfConsumer = CGDataConsumer(data: pdfData as CFMutableData)!
           var mediaBox = CGRect.init(x: 0, y: 0, width: image.size.width, height: image.size.height)
           let pdfContext = CGContext(consumer: pdfConsumer, mediaBox: &mediaBox, nil)!
           pdfContext.beginPage(mediaBox: &mediaBox)
           pdfContext.draw(image.cgImage!, in: mediaBox)
           pdfContext.endPage()
           return pdfData
       }
0 Answers
Related