美文网首页iOS开发
iOS 截屏、图片转换PDF工具方法

iOS 截屏、图片转换PDF工具方法

作者: _浅墨_ | 来源:发表于2023-07-28 09:51 被阅读0次
    private func screenshot() -> UIImage? {
            let controller = UIHostingController(rootView: self)
            let view = controller.view
    
            let targetSize = UIScreen.main.bounds.size
            view?.frame = CGRect(origin: .zero, size: targetSize)
            view?.backgroundColor = .clear
    
            let renderer = UIGraphicsImageRenderer(size: targetSize)
            return renderer.image { _ in
                view?.drawHierarchy(in: controller.view.bounds, afterScreenUpdates: true)
            }
        }
        
        private func imageToPDFData(image: UIImage) -> Data? {
            let pdfRenderer = UIGraphicsPDFRenderer(bounds: CGRect(origin: .zero, size: image.size))
            let pdfData = pdfRenderer.pdfData { (context) in
                context.beginPage()
                image.draw(in: CGRect(origin: .zero, size: image.size))
            }
            return pdfData
        }
    

    相关文章

      网友评论

        本文标题:iOS 截屏、图片转换PDF工具方法

        本文链接:https://www.haomeiwen.com/subject/qtgrpdtx.html