Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import PlaygroundSupport
import SwiftUI
extension UIView {
var renderedImage: UIImage {
let image = UIGraphicsImageRenderer(size: self.bounds.size).image { context in
UIColor.lightGray.set(); UIRectFill(bounds)
context.cgContext.setAlpha(0.75)
self.layer.render(in: context.cgContext)
}
return image
}
}
extension View {
var renderedImage: UIImage {
let window = UIWindow(frame: CGRect(origin: .zero, size: CGSize(width: 320, height: 160)))
let hosting = UIHostingController(rootView: self)
hosting.view.frame = window.frame
window.rootViewController = hosting
window.makeKey()
return hosting.view.renderedImage
}
}
Text("Hello").renderedImage
Slider(value: .constant(0.5)).renderedImage
let img = ([Color.red, .orange, .yellow, .green, .blue, .purple]
.reduce(AnyView(Text("👭").font(.largeTitle)
.rotationEffect(Angle(radians: .pi)))) {
AnyView($0.padding()
.background($1)
.rotationEffect(Angle(radians: .pi / 6)))
}).renderedImage
img
@alexbbrown

This comment has been minimized.

Copy link

@alexbbrown alexbbrown commented Jun 21, 2019

Well, on Catalina I can do this. I haven't tried Mojave.

let view = ([Color.red, .orange, .yellow, .green, .blue, .purple]
    .reduce(AnyView(Text(":-)").font(.largeTitle)
        .rotationEffect(Angle(radians: .pi)))) {
            AnyView($0.padding()
                .background($1)
                .rotationEffect(Angle(radians: .pi / 6)))
})

PlaygroundPage.current.liveView = UIHostingController(rootView: view)
@alexbbrown

This comment has been minimized.

Copy link

@alexbbrown alexbbrown commented Jun 21, 2019

but I see you already know how to do that: https://ericasadun.com/2019/06/06/good-things-swiftui-on-mojave-in-ios-playgrounds/

But on the other hand I got to execute some swiftUI for the first time. Thanks!

@DonSwet

This comment has been minimized.

Copy link

@DonSwet DonSwet commented Aug 17, 2019

(You can’t conform View and if you conform the individual types, it doesn’t work.)

Maybe it's because it's a later beta, but for me this code works perfectly fine in Mojave:
Live Preview for SwiftUI in Mojave!

@snyeah

This comment has been minimized.

Copy link

@snyeah snyeah commented Dec 1, 2019

I'm trying to render a SwiftUI View into a UIImage. This code works perfectly in Swift Playground. But in a simulator or real device, it generates a blank image. Any idea how could make it work please?

This is the code I'm using

let size = // Calcuate size //
let hosting = UIHostingController(rootView: self)
hosting.view.frame = CGRect(origin: .zero, size: size)
let image = hosting.view.renderedImage

@erica

This comment has been minimized.

Copy link
Owner Author

@erica erica commented Dec 6, 2019

@snyeah

This comment has been minimized.

Copy link

@snyeah snyeah commented Dec 6, 2019

I have a “report card” on screen which is a SwiftUI view. I want to use the export button to convert it to a UIImage then save to camera roll and / or send out the jpeg via email. Does SwiftUI view has a layer property or similar thing that I can work with CGContext?

@pakLebah

This comment has been minimized.

Copy link

@pakLebah pakLebah commented Dec 12, 2019

I'm using XCode 11.3 on Mojave. None of these work. Could you please update the code? Thank you.

@erica

This comment has been minimized.

Copy link
Owner Author

@erica erica commented Aug 18, 2020

Leaving myself a note:

import PlaygroundSupport
import SwiftUI
extension UIView {
    var renderedImage: UIImage {
      let image = UIGraphicsImageRenderer(size: bounds.size).image { context in
        layer.render(in: context.cgContext)
      }
      return image
    }

    var renderedPlaygroundImage: UIImage {
    let image = UIGraphicsImageRenderer(size: bounds.size).image { context in
      UIColor.lightGray.set(); UIRectFill(bounds)
      context.cgContext.setAlpha(0.75)
      self.layer.render(in: context.cgContext)
    }
    return image
  }
}
extension View {
  var renderedImage: UIImage {
    let window = UIWindow(frame: CGRect(origin: .zero, size: CGSize(width: 320, height: 160)))
    let hosting = UIHostingController(rootView: self)
    hosting.view.frame = window.frame
    window.rootViewController = hosting
    window.makeKey()
    return hosting.view.renderedImage
  }
}

Text("Hello").renderedImage

Slider(value: .constant(0.5)).renderedImage
let img = ([Color.red, .orange, .yellow, .green, .blue, .purple]
  .reduce(AnyView(Text("👭").font(.largeTitle)
    .rotationEffect(Angle(radians: .pi)))) {
      AnyView($0.padding()
        .background($1)
        .rotationEffect(Angle(radians: .pi / 6)))
}).renderedImage
img
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.