Tuesday, March 12, 2024
HomeiOS Developmentios - How can I effectively render many pages of SwiftUI to...

ios – How can I effectively render many pages of SwiftUI to pdf?


I’m making an attempt to create a PDF doc from tons of of pages of a SwiftUI view’s. Presently my method is the next. This works effective however it’s fairly sluggish. Was questioning if there’s a higher means of doing this both through the use of UIKit or another methodology?

@MainActor
func renderPages() async {
    let pathComponent = "instance.pdf"
    let url = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(pathComponent)
    var field = CGRect(x: 0, y: 0, width: 1000, peak: 500)
    guard let pdf = CGContext(url as CFURL, mediaBox: &field, nil) else {
        return
    }
    for pageIndex in pageIndices {
        pdf.beginPDFPage(nil)
        let renderer = ImageRenderer(content material:
             MySwiftUIView(index: pageIndex)
        )
        renderer.render { measurement, myContext in
            myContext(pdf)
        }
        pdf.endPDFPage()
    }
    pdf.closePDF()
    self.url = url
}



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments