3

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

我也尝试了以下解决方案

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

这是我的代码

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,这里我附上屏幕截图,以便您可以轻松理解这是我的输出,现在我想用这个别针另存为 pdf

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

4

2 回答 2

1

使用此代码将 pdf 文件成功保存在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)
    }
}

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

在此处输入图像描述

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

输出路径打印如

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

如果您使用真实设备运行,则可以使用代码进行检查,请参阅此帖子以检查设备中是否存在文件 如何检查 Swift 中的 Documents 目录中是否存在文件?

于 2019-02-22T07:14:46.537 回答
0

要在点击保存按钮时获取 imageView 的上下文,您可以更新您的 create 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
}
于 2019-02-22T08:02:39.253 回答