您好,我正在将 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,这里我附上屏幕截图,以便您可以轻松理解
所以请告诉我这是正确的方法或者我做错了什么请告诉我