Skip to content

How to use EFQRCode in SwiftUI #129

Answered by ApolloZhu
ApolloZhu asked this question in Q&A
Discussion options

You must be logged in to vote

EFQRCode generates a CGImage on success, so ideally we should be able to make a SwiftUI Image from the result like this:

struct ContentView: View {
    var body: some View {
        if let qrCode = EFQRCode.generate(for: "Hello World") {
            Image(cgImage: qrCode)
                .resizable()
                .aspectRatio(1, contentMode: .fit)
        } else {
            Text("Generation Failed")
        }
    }
}

However, since SwiftUI doesn't support doing that, we need to add the following initializer extension ourselves:

extension Image {
    init(cgImage: CGImage) {
        #if os(macOS)
        self.init(nsImage: NSImage(cgImage: cgImage,
                                   size

Replies: 1 comment 1 reply

Comment options

ApolloZhu
Aug 6, 2021
Maintainer Author

You must be logged in to vote
1 reply
@AhmadKsb
Comment options

Answer selected by ApolloZhu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants