I'm getting a low quality image when using ImageRenderer on iOS16 both on simulator and device. i.e. when saving the rendered image to the Photo Library or sending to Notes.. it's very pixelated.
Everything I read would suggest simply setting .scale but that appears to have no effect.
I'm including a sample project below and on GitHub. You can see the commented out sections which also fail.
It would seem that scale is being ignored completely.
Thanks in advance for your observations.
PS. The reason I'm providing a preview to the user is because in the preview I'm providing the user the ability to add some custom text which would not normally be in the regular UI. There'd be an update button to re-render to the preview.
import SwiftUI
struct helloWorldView: View {
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundColor(.accentColor)
Text("Hello, world!")
}
}
}
struct ContentView: View {
@State private var screenshotimage: UIImage?
@State private var screenshot: Bool = false
@State private var showsharesheet: Bool = false
@State private var sharescreenshot: Bool = false
@State private var imageToShare: Image?
var body: some View {
NavigationStack {
helloWorldView()
.padding()
.toolbar {
ToolbarItem(placement: .primaryAction) {
Button("Share") {
showsharesheet.toggle()
}
}
}
.sheet(isPresented: self.$showsharesheet) {
NavigationStack {
ScrollView {
Section {
if screenshotimage != nil {
Image(uiImage: screenshotimage!)
ShareLink(
item: Image(uiImage: screenshotimage!),
preview: SharePreview(
"Share Title",
image: Image(uiImage: screenshotimage!)
)
) {
Label("Share Image", systemImage: "square.and.arrow.up")
.foregroundColor(.white)
.padding()
.background(.blue.gradient.shadow(.drop(radius: 1, x: 2, y: 2)), in: RoundedRectangle(cornerRadius: 5))
}
} else {
Text("Creating image..")
}
}
}
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Dismiss") {
showsharesheet = false
}
}
}
.navigationTitle("Preview")
.navigationBarTitleDisplayMode(.inline)
}
.onAppear() {
screenshot.toggle()
}
.onChange(of: screenshot, perform: { _ in
// Task {
let renderer = ImageRenderer(content:helloWorldView())
// renderer.scale = UIScreen.main.scale
renderer.scale = 3.0
screenshotimage = renderer.uiImage
// }
})
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}