Created
April 6, 2021 15:09
-
-
Save prafullakumar/be03a91609d89d15cad431de94c930cf to your computer and use it in GitHub Desktop.
This file contains hidden or 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 SwiftUI | |
struct StackGeometryReader: View { | |
@Binding var takePic: Bool | |
@State private var contentHeight: CGFloat = 0 | |
var body: some View { | |
VStack { | |
PrintableCells(color: Color.pink) | |
PrintableCells(color: Color.red) | |
PrintableCells(color: Color.green) | |
PrintableCells(color: Color.pink) | |
PrintableCells(color: Color.red) | |
PrintableCells(color: Color.green) | |
PrintableCells(color: Color.pink) | |
PrintableCells(color: Color.red) | |
PrintableCells(color: Color.green) | |
} | |
.padding() | |
.background( | |
ZStack { | |
GeometryReader { proxy in | |
Color.clear.onAppear() { | |
contentHeight = proxy.size.height | |
}.onChange(of: takePic) { (newVal) in | |
if newVal { | |
let image = self.takeScreenshot(origin: proxy.frame(in: .global).origin, size: proxy.size) | |
print(image) // | |
takePic.toggle() | |
} | |
} | |
} | |
}) | |
} | |
} | |
struct ContentView: View { | |
@State private var takePic = false | |
var body: some View { | |
NavigationView { | |
ScrollView { | |
StackGeometryReader.init(takePic: $takePic) | |
}.navigationTitle("Demo Screen Shot") | |
.navigationBarItems(leading: Button(action: { | |
takePic.toggle() | |
}, label: { | |
Text("Take pic") | |
})) | |
} | |
} | |
} | |
struct PrintableCells: View { | |
var color: Color | |
var body: some View { | |
GeometryReader { proxy in | |
VStack { | |
Button(action: { | |
let image = self.takeScreenshot(origin: proxy.frame(in: .global).origin, size: proxy.size) | |
print(image) // | |
}) { | |
RoundedRectangle(cornerRadius: 20) | |
.fill(self.color) | |
.overlay( | |
VStack { | |
Text("size: \(proxy.size.debugDescription)") | |
.foregroundColor(.white) | |
})} | |
} | |
}.frame(height: 100) | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} | |
extension UIView { | |
var screenShot: UIImage { | |
let rect = self.bounds | |
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0) | |
let context: CGContext = UIGraphicsGetCurrentContext()! | |
self.layer.render(in: context) | |
let capturedImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()! | |
UIGraphicsEndImageContext() | |
return capturedImage | |
} | |
} | |
extension View { | |
func takeScreenshot(origin: CGPoint, size: CGSize) -> UIImage { | |
let window = UIWindow(frame: CGRect(origin: origin, size: size)) | |
let hosting = UIHostingController(rootView: self) | |
hosting.view.frame = window.frame | |
window.addSubview(hosting.view) | |
window.makeKeyAndVisible() | |
return hosting.view.screenShot | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment