-
-
Save wb-towa/b6d8e1741dafbc0851f5ceb050b1aa18 to your computer and use it in GitHub Desktop.
Take capture a view by SwiftUI
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
// | |
// ContentView.swift | |
// TryGeometryReader | |
// | |
// Created by satoutakeshi on 2019/12/07. | |
// Copyright © 2019 satoutakeshi. Licensed under MIT. | |
// | |
import SwiftUI | |
struct ContentView: View { | |
var body: some View { | |
ScrollView { | |
VStack { | |
Text("GeometryReader Get Grobal Origin") | |
GeometryRectangle(color: Color.pink) | |
GeometryRectangle(color: Color.red) | |
.offset(x: 10, y: 0) | |
ZStack { | |
GeometryRectangle(color: Color.blue) | |
.offset(x: 30, y: 0) | |
} | |
} | |
} | |
} | |
} | |
struct GeometryRectangle: View { | |
var color: Color | |
var body: some View { | |
GeometryReader { geometry in | |
VStack { | |
Button(action: { | |
let image = self.takeScreenshot(origin: geometry.frame(in: .global).origin, size: geometry.size) | |
print(image) | |
}) { | |
RoundedRectangle(cornerRadius: 20) | |
.fill(self.color) | |
.overlay( | |
VStack { | |
Text("X: \(Int(geometry.frame(in: .global).origin.x)) Y: \(Int(geometry.frame(in: .global).origin.y)) width: \(Int(geometry.frame(in: .global).width)) height: \(Int(geometry.frame(in: .global).height))") | |
.foregroundColor(.white) | |
Text("size: \(geometry.size.debugDescription)") | |
.foregroundColor(.white) | |
})} | |
} | |
}.frame(height: 100) | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} | |
extension UIView { | |
var renderedImage: UIImage { | |
// rect of capure | |
let rect = self.bounds | |
// create the context of bitmap | |
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0) | |
let context: CGContext = UIGraphicsGetCurrentContext()! | |
self.layer.render(in: context) | |
// get a image from current context bitmap | |
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.renderedImage | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment