2

Hello i am opening pdf from file to imageview and then on image view i am dropping pin and after that i want to save again as pdf but its not saved in files on iphone

i also tried below solution

https://stackoverflow.com/a/45001576/10804348

Here Is My Code

import UIKit
import PDFKit

class PreviewViewController: UIViewController {

    @IBOutlet private weak var imageView: UIImageView!

    var image: UIImage?

    override func viewDidLoad() {
        super.viewDidLoad()
        let longP = UILongPressGestureRecognizer(target: self, action: #selector(longPressed(sender:)))
        self.imageView.addGestureRecognizer(longP)
        imageView.isUserInteractionEnabled = true

        imageView.image = image
    }
    @objc func longPressed(sender: UILongPressGestureRecognizer) {
        print("longpressed")
        let touchPoint = sender.location(in: self.imageView)
        let xPOint = touchPoint.x
        let yPoint = touchPoint.y
        let buble = UIImageView(frame: CGRect(x: xPOint, y: yPoint, width: 20, height: 20))
        buble.image = UIImage(named: "Pin")
        imageView.addSubview(buble)
    }
    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
    }
    @IBAction func btnSaveTapped(_ sender: UIButton) {
        let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
        let docURL = documentDirectory.appendingPathComponent("myFileName.pdf")

         createPDF(image: image!)?.write(to: docURL, atomically: true)
        print("Saved")
    }
}

but still i am not able to save as pdf in device files, here i am attaching screen shot So You can understand easily Here Is my OutPut and now i want to save as pdf with this pin

So pelase tell me this is the right way or where i am done wrong please tell me

Vishal Parmar
  • 615
  • 4
  • 31

2 Answers2

1

Use this code to save pdf file successfully at FileManager.

@IBAction func btnSaveTapped(_ sender: UIButton) {
let fileManager = FileManager.default
    do {
        let documentDirectory = try fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor:nil, create:false)
        let docURL = documentDirectory.appendingPathComponent("myFileName.pdf")
        let myImage = UIImage(named: "Screen1")
        print(docURL)
        createPDF(image: myImage!)?.write(to: docURL, atomically: true)
    } catch {
        print(error)
    }
}

See Saved file in your file manager

enter image description here

Follow this step to show file at the path (when run with simulator)

Output path print like

file:///Users/atulparmar/Library/Developer/CoreSimulator/Devices/D8012A46-71C3-4A99-8D81-31890208EF03/data/Containers/Data/Application/506B9D5A-9FB0-4865-8F14-D2DB27495B05/Documents/myFileName.pdf

open Finder -> Go -> Go to Folder.. past path like following (shortcut ⇧⌘G)

~/Library/Developer/CoreSimulator/Devices/D8012A46-71C3-4A99-8D81-31890208EF03/data/Containers/Data/Application/506B9D5A-9FB0-4865-8F14-D2DB27495B05/Documents/myFileName.pdf

If you are run with the real device then you can check with code, see this post to check file exist or not in device How to check if a file exists in the Documents directory in Swift?

AtulParmar
  • 4,358
  • 1
  • 24
  • 45
0

To get the context of the imageView at the moment the save button is tapped, you can update your create pdf function to render the imageview layer into a pdf context when the button is tapped. The resulting pdf contains the image plus the pins you have added:

func createPDF() -> NSData? {
   let pdfData = NSMutableData()
    UIGraphicsBeginPDFContextToData(pdfData, imageView.bounds, nil)
    UIGraphicsBeginPDFPage()
    let pdfContext = UIGraphicsGetCurrentContext()
    imageView.layer.render(in: pdfContext!)
    UIGraphicsEndPDFContext()
    return pdfData
}
adri
  • 265
  • 2
  • 5