【发布时间】:2019-08-31 02:52:20
【问题描述】:
我在 UIScrollView 内创建了几个 UIView,它们根据我在 Height 和 Width 文本字段中键入的值动态调整大小。一旦 UIView 调整大小,我将 UIScrollView 的内容保存为 PDF 数据。
我发现 PDF 中 UIView 的尺寸(在 Adobe Illustrator 中测量时)总是四舍五入到三分之一。
例如:
1.5 -> 1.333
1.75 -> 1.666
每次更新约束之前,我都会检查常量值,它们是准确的。谁能解释为什么 UIView 在呈现为 PDF 后尺寸不正确?
@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 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)
}
}
【问题讨论】:
标签: ios swift uiview pdf-generation nslayoutconstraint