-
Currently, there are no examples in, nor native support for SwiftUI |
Beta Was this translation helpful? Give feedback.
Answered by
ApolloZhu
Aug 6, 2021
Replies: 1 comment 1 reply
-
EFQRCode generates a 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: NSSize(width: cgImage.width,
height: cgImage.height)))
#else
self.init(uiImage: UIImage(cgImage: cgImage))
#endif
}
} Then, you can display the generated QRCode with SwiftUI (on any platform supported by SwiftUI and EFQRCode, such as watchOS):
|
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
ApolloZhu
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
EFQRCode generates a
CGImage
on success, so ideally we should be able to make a SwiftUIImage
from the result like this:However, since SwiftUI doesn't support doing that, we need to add the following initializer extension ourselves: