Skip to content

Instantly share code, notes, and snippets.

Last active August 31, 2023 09:10
Show Gist options
  • Star 39 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save SatoTakeshiX/39e111aa93f896e58c74fdc13d19ba0a to your computer and use it in GitHub Desktop.
Save SatoTakeshiX/39e111aa93f896e58c74fdc13d19ba0a to your computer and use it in GitHub Desktop.
Take capture a view by SwiftUI
// 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")
.offset(x: 10, y: 0)
ZStack {
.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)
}) {
RoundedRectangle(cornerRadius: 20)
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))")
Text("size: \(geometry.size.debugDescription)")
}.frame(height: 100)
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
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()!
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
return hosting.view.renderedImage
Copy link

Thank you Satoさん.
But I still have a question.
Now I have a ScrollView embedded inside a GeometryReader, and the content size of the ScrollView has a height that is greater than the size of the GeometryProxy, how can I get the full scrollable content be converted to image instead of the visible contents only?

Copy link

keatsp commented Oct 18, 2020

This used to work amazing for my app. Thank you very much for that. However since iOS14 I get an error at self.layer.render(in: context). AttributeGraph precondition failure: setting value during update: 22552.

The app freezes and xCode says the typical Thread 1: signal SIGABRT

Any help would be greatly appreciated.

Copy link

wingovers commented Dec 20, 2020

@keatsp can you post code somewhere? works fine for me

Copy link

I get a similar issue. I am trying to take a screenshot of a view that is not being displayed. This is probably the issue. I was attempting to take a "preview" of a view to display in a context menu. I am getting this error:

2021-04-11 22:20:44.770952-0400 _____[1967:259741] [error] precondition failure: setting value during update: 764440
AttributeGraph precondition failure: setting value during update: 764440.


Thread 1: signal SIGABRT

Copy link

I have the same issue as @claytonsulby and @keatsp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment