您好,我要打开从文件到imageview的pdf文件,然后在图像视图中放下别针,然后我要再次另存为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,我在这里附加了屏幕截图,所以您可以轻松理解
请告诉我这是正确的方法,否则我做错了请告诉我
答案 0 :(得分: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->转到->转到文件夹..经过下面的路径(快捷键⇧⌘G)
〜/ Library / Developer / CoreSimulator / Devices / D8012A46-71C3-4A99-8D81-31890208EF03 / data / Containers / Data / Application / 506B9D5A-9FB0-4865-8F14-D2DB27495B05 / Documents / myFileName.pdf
如果您在真实设备上运行,则可以使用代码进行检查,请参阅此帖子以检查设备How to check if a file exists in the Documents directory in Swift?中文件是否存在
答案 1 :(得分: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
}