skip to Main Content

while we make a pdf using an image file at that we add an image into PDFPage. at that time PDFDocument did not show the original image content in PDFView in iOS 16. I checked the same code which worked properly in iOS 15. For both test cases, I used New Xcode 14, and as well as previously uploaded TestFlight build also worked properly with iOS 15 or below but did not work with iOS 16. So I request to the apple developer who worked on PDFkit please check this case. I may be sure that there is an issue with iOS 16 and I hope apple provides a new iOS 16 sub-version release as soon as possible with includes this bug fix. here is the sample code that I used:-

let images = [UIImage]() 
let pdfDocument = PDFDocument() 
for (index,image) in images.enumerated(){ 
   // Create a PDF page instance from your image 
   let pdfPage = PDFPage(image: image)
   // Insert the PDF page into your document
   pdfDocument.insert(pdfPage!, at: index) 
}

Original Image:

Original Image

PDF preview in PDFView:

PDF preview Image in PDFView

2

Answers


  1. My guess is there’s an issue with the default color space in PDFKit: RGB vs CMYK

    Here’s a workaround:

    class func imageToPDF(_ image:UIImage) -> Data {
        let data = NSMutableData()
            
        let bounds = CGRect(origin: CGPoint.zero, size: image.size)
            
        UIGraphicsBeginPDFContextToData(data, bounds, nil)
        UIGraphicsBeginPDFPage()
        image.draw(at: CGPoint.zero)
        UIGraphicsEndPDFContext()
            
        return data as Data
    }
    

    You’ll have to alter it for multiple pages in your instance

    Login or Signup to reply.
  2. Try to remove alpha channel from the image before inserting it into PDFPage. Below is an example how to remove alpha channel from UIImage. Tested with iOS 16.1.

    extension UIImage {
        func removeAlpha() -> UIImage {
            let format = UIGraphicsImageRendererFormat()
            format.opaque = true
            format.scale = scale
            return UIGraphicsImageRenderer(size: size, format: format).image { _ in
                draw(in: CGRect(origin: .zero, size: size))
            }
        }
    }
    
    struct PhotoDetailView: UIViewRepresentable {
        let image: UIImage
        
        func makeUIView(context: Context) -> PDFView {
            let view = PDFView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height))
            view.document = PDFDocument() 
            // {{ REMOVE ALPHA BEFORE INSERTING TO PDF
            guard let page = PDFPage(image: image.removeAlpha()) else { return view }
            // }}
            view.document?.insert(page, at: 0)
            view.pageBreakMargins = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
            view.minScaleFactor = 1
            view.maxScaleFactor = 10
            view.autoScales = true
            view.backgroundColor = .clear
            return view
        }
    
        func updateUIView(_ uiView: PDFView, context: Context) {        
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search