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
}
网友评论