-1

The following function converts the pages of a pdf document into NSImages.

    @available(macOS 13.0, *)
func convertPDFPagesToImages(pdfDoc: PDFDocument)-> [NSImage] {
    
    let pageCount = pdfDoc.pageCount
    var images: [NSImage] = []
    for i in 0..<pageCount {
        let page = pdfDoc.page(at: i)
        let pdfKitView = PDFKitView(pdfDoc: pdfDoc, viewSize: CGSize(width: 600, height: 814), currentPage: .constant(page))
        let renderer = ImageRenderer(content: pdfKitView)
        if let cgImage = renderer.cgImage {
            let image = NSImage(cgImage: cgImage, size: NSSize(width: 600, height: 814))
            images.append(image)
        }
        
    }
    
    return images
}

Error: Cannot find 'ImageRenderer' in scope.

I have imported SwiftUI. According to Apple's documentation, Image renderer should be available from macOS 13. Is this a bug or have I misunderstood the @available decorator?

domi852
  • 497
  • 1
  • 4
  • 13
  • Addendum: obviously, the whole function is in a preprocessor condition (#if canImport(AppKit) ...). – domi852 Jan 05 '23 at 10:59
  • 1
    This works fine for me when using ImageRender in a function marked with `@available(macOS 13.0, *)` and building for 12.4. I think your error is caused by something else, for instance if you call the init with an incorrect type it might confuse the compiler saying it doesn't know what ImageRenderer you mean. – Joakim Danielson Jan 05 '23 at 11:11
  • 1
    What version of Xcode are you using exactly? – Sweeper Jan 05 '23 at 11:16
  • thanks for the answer - the problem was indeed my Xcode version... Didn't think of it because with the iOS target, the compiler found the ImageRenderer without problem... – domi852 Jan 05 '23 at 11:40

1 Answers1

1

Please check your Xcode version. Xcode 14.1 is the first version to support macOS 13 development. Xcode 14.0 only has support for macOS 12.3. So on Xcode 14.1+, your code should compile fine.

On the other hand, Xcode 14.0 does support iOS 16.0, so it is possible to use ImageRenderer on the iOS platform.

You are free to write any reasonable-looking version in the @available attribute, but if you don't have the SDK, you of course cannot use anything from that version.

Sweeper
  • 213,210
  • 22
  • 193
  • 313