【发布时间】:2019-08-31 00:31:30
【问题描述】:
我有几个UIViews,背景颜色清晰,黑色边框轮廓都根据我在长度、宽度和高度文本字段中输入的值调整大小。这些视图是UIScrollView 的子视图。视图调整大小后,我从滚动视图的内容创建一个 PDF,生成可以在 Adobe Illustrator 中使用和操作的矢量化轮廓。
当我为视图使用小尺寸(即 15 x 15)时,此过程可以正常工作,但是当我为我的目的使用更理想的尺寸(即 3000 x 3000)时,视图会超出屏幕的边界并导致生成的 PDF 仅包含一小部分视图。
理想情况下,我需要能够创建更大的视图,并且仍然能够生成包含它们的 PDF。我正在设想一个画布,它可以根据其内容调整大小(缩小?)并保留其子视图的比例,以便在 Adobe Illustrator 中查看时 PDF 中的结果视图将按比例缩放。到目前为止,这是我的代码:
override func viewDidLoad() {
super.viewDidLoad()
lengthTextField.delegate = self
widthTextField.delegate = self
heightTextField.delegate = self
scrollView.delegate = self
scrollView.autoresizingMask = [UIView.AutoresizingMask.flexibleWidth,UIView.AutoresizingMask.flexibleHeight]
scrollView.minimumZoomScale = 1
scrollView.maximumZoomScale = 50
scrollView.zoomScale = 1
self.setupGestureRecognizer()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
scrollView.contentSize = CGSize(width: 5000, height: 5000)
}
func setupGestureRecognizer() {
}
@IBAction func viewPDF(_ sender: Any) {
createPDFfrom(aView: self.scrollView.subviews[0], saveToDocumentsWithFileName: "MC.pdf")
// Create and add a PDFView to the view hierarchy.
let pdfView = PDFView(frame: self.scrollView.subviews[0].bounds)
pdfView.autoScales = true
view.addSubview(pdfView)
// Create a PDFDocument object and set it as PDFView's document to load the document in that view.
let documentsDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let filePath = (documentsDirectory as NSString).appendingPathComponent("MC.pdf") as String
let pdfDocument = PDFDocument(url: URL(fileURLWithPath: filePath))!
pdfView.document = pdfDocument
let document = NSData(contentsOfFile: filePath)
let vc = UIActivityViewController(activityItems: [document as Any], applicationActivities: nil)
self.present(vc, animated: true, completion: nil)
}
func createPDFfrom(aView: UIView, saveToDocumentsWithFileName fileName: String)
{
let pdfData = NSMutableData()
UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil)
UIGraphicsBeginPDFPage()
guard let pdfContext = UIGraphicsGetCurrentContext() else { return }
aView.layer.render(in: pdfContext)
UIGraphicsEndPDFContext()
if let documentDirectories = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first {
let documentsFileName = documentDirectories + "/" + fileName
debugPrint(documentsFileName)
pdfData.write(toFile: documentsFileName, atomically: true)
}
}
@IBAction func updateDimensions(_ sender: Any) {
guard let length = NumberFormatter().number(from:
lengthTextField.text ?? "") else { return }
guard let width = NumberFormatter().number(from:
widthTextField.text ?? "") else { return }
guard let height = NumberFormatter().number(from:
heightTextField.text ?? "") else { return }
let flapHeight = CGFloat(truncating: width)/2
let lengthFloat = CGFloat(truncating: length)
let widthFloat = CGFloat(truncating: width)
let heightFloat = CGFloat(truncating: height)
UIView.animate(withDuration: 0.3) {
self.faceAWidthConstraint.constant = lengthFloat
self.faceAHeightConstraint.constant = heightFloat
self.faceBWidthConstraint.constant = widthFloat
self.faceA1HeightConstraint.constant = flapHeight
self.view.layoutIfNeeded()
}
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return scrollView.subviews[0]
}
}
【问题讨论】:
标签: ios swift uiview uiscrollview pdf-generation