将图像转换为 pdf 但不保存在文件中

converting Image to pdf but not save in files

你好,我正在从文件打开 pdf 到图像视图,然后在图像视图上我正在删除图钉,之后我想再次保存为 pdf,但它没有保存在 iphone

上的文件中

我也尝试了以下解决方案

这是我的代码

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")
    }
}

但我仍然无法在设备文件中另存为 pdf,这里我附上屏幕截图以便您轻松理解

所以请告诉我这是正确的方法或者我哪里做错了请告诉我

使用此代码在 FileManager 成功保存 pdf 文件。

@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)
    }
}

在文件管理器中查看保存的文件

按照此步骤在路径中显示文件(当 运行 使用模拟器时)

输出路径打印如

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

打开 Finder -> Go -> Go to Folder.. 路径如下(快捷键 ⇧⌘G)

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

如果您 运行 使用的是真实设备,那么您可以使用代码进行检查,查看此 post 以检查设备中是否存在文件 How to check if a file exists in the Documents directory in Swift?

要在点击保存按钮时获取 imageView 的上下文,您可以更新创建 pdf 函数以在点击按钮时将 imageview 层渲染到 pdf 上下文中。生成的 pdf 包含图像和您添加的图钉:

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