如何缩放UIVIew以适应A4纸张大小

4

我正在开发一个将UIView转换为PDF文件并适合A4纸张大小进行打印的应用程序。

如何缩放任何UIView以适合A4纸张大小?

我需要我的UIView完全适合A4页面。


在一个固定的A4纸大小框架内,将您的视图添加到滚动视图中。 - Muhammad Adnan
使用UIGraphicsBeginPDFContextToFile将其转换为PDF并打印。 https://dev59.com/2Ggt5IYBdhLWcg3wywdJ - Ashutosh Dave
我想将UIVIew缩放到A4纸张大小。 - Janak Thakkar
如果您的UIView大小为1024x768,而您的PDF页面大小为792x612,则为确保PDF输出中可见所有UIView,必须适当缩放上下文。 792/1023 = 0.733,这是缩放因子:CGContextScaleCTM(pdfContext,0.773f,0.773f);
  • 如@Lion答案中提供的链接建议
- Ashutosh Dave
1个回答

2

您可以将类似以下内容进行转换:

    func createPdfFromView(aView: UIView, saveToDocumentsWithFileName fileName: String)
{
    let pdfData = NSMutableData()
    UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil)
    UIGraphicsBeginPDFPage()

    guard let pdfContext = UIGraphicsGetCurrentContext() else { return }

    aView.layer.renderInContext(pdfContext)
    UIGraphicsEndPDFContext()

    if let documentDirectories = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first {
        let documentsFileName = documentDirectories + "/" + fileName
        debugPrint(documentsFileName)
        pdfData.writeToFile(documentsFileName, atomically: true)
    }
}

阅读这篇文章获取更多细节,并学习如何在不同尺寸上呈现它。


这个很完美,但是它不能将UIVIew缩放以适应A4纸张大小。 - Janak Thakkar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接