swiftpdfuiimagepickercontrollerpdfkitpdfpages

The page is not added to PDF, it gives an error "Optinal value"?


Good afternoon, I want to call a function in a button to add an image page to a document.Here in this method which provided below.

@IBAction func mergeButton(_ sender: Any) {
  
  
        let newDocument = PDFDocument()
        let image = UIImage(systemName: "image")
      
        let page = PDFPage(image: image!)
        
        newDocument.insert(page!, at: 0)
        newPDF.document = newDocument
      
    }

This is my entire code. I was going to upload the image via picker and then add it to the pdf document. Can anyone help me correct what is wrong? Below is my entire code

import UIKit
import PDFKit
import MobileCoreServices

class ViewController: UIViewController,UINavigationControllerDelegate, UIImagePickerControllerDelegate,PDFViewDelegate {
  
    
    let pdf = PDFView()
    let newPDF = PDFView()
    
    @IBOutlet weak var pdfImage: UIView!
    
   
    
    @IBOutlet weak var imageGallery: UIImageView!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
      pdfImage.addSubview(pdf)
        mergeFile.addSubview(newPDF)
        guard let url = Bundle.main.url(forResource: "foglio", withExtension: "pdf") else {
            return
        }
        
        
        guard let document = PDFDocument(url: url) else {return }
        
        pdf.document = document
        pdf.autoScales = true
        pdf.delegate = self
        newPDF.delegate = self
            
        
        
    }
  
    
    
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        pdf.frame = pdfImage.bounds
        pdf.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        newPDF.frame = mergeFile.bounds
        newPDF.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        
    }

   
    @IBOutlet weak var mergeFile: UIView!
    
    
    @IBAction func mergeButton(_ sender: Any) {
  
  
        let newDocument = PDFDocument()
        let image = UIImage(systemName: "image")
      
        let page = PDFPage(image: image!)
        
        newDocument.insert(page!, at: 0)
        newPDF.document = newDocument
      
    }
    
    @IBAction func ImageUpdate(_ sender: Any) {
        let picker = UIImagePickerController()
            picker.delegate = self
            picker.sourceType = .photoLibrary

        present(picker, animated: true, completion: nil)
    }
    
    
     private func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

        if let image = info[UIImagePickerController.InfoKey.originalImage.rawValue] as? UIImage {
           
            imageGallery.image = image

        }
         self.dismiss(animated: true, completion: nil)
    }


     func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {

         self.dismiss(animated: true, completion: nil)
    }

  

   
}

Solution

  • try something like this:

        let newDocument = PDFDocument()
        if let image = UIImage(named: "image"),
           let page = PDFPage(image: image) {
            newDocument.insert(page, at: 0)
            newPDF.document = newDocument
        }