Skip to content

Instantly share code, notes, and snippets.

@andresr-dev
Last active May 5, 2023 17:35
Show Gist options
  • Save andresr-dev/373bc550d88893ec91af9b0186825b06 to your computer and use it in GitHub Desktop.
Save andresr-dev/373bc550d88893ec91af9b0186825b06 to your computer and use it in GitHub Desktop.
This is how to convert a SwiftUI View to Base 64 format.
import SwiftUI
struct SwiftUIToBase64View: View {
@Environment(\.displayScale) var displayScale
var body: some View {
Text("Hello, World!")
}
@MainActor private func ecode() -> String? {
let renderer = ImageRenderer(content: Text("Hello"))
// Make sure to use the correct display scale for this device, so that the image can render with good resolution.
renderer.scale = displayScale
return renderer.uiImage?.jpegData(compressionQuality: 1.0)?.base64EncodedString()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment