Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Create snapshot using drawHierarchy() to include child views
// UIView+Ext.swift
// tahrir
// Created by Maysam Shahsavari on 9/14/19.
// Copyright © 2019 Maysam Shahsavari. All rights reserved.
import Foundation
import UIKit
extension UIView {
func snapshot() -> UIImage? {
UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0.0)
drawHierarchy(in: CGRect(origin:, size: bounds.size), afterScreenUpdates: true)
if let image = UIGraphicsGetImageFromCurrentImageContext() {
return image
return nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment