-
-
Save alexbbrown/1d8ad4b1db5a1b7ef126834c5d15e579 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 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) | |
Author
alexbbrown
commented
Jun 21, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment