Skip to content

Instantly share code, notes, and snippets.

@giovani-pereira-ifood
Last active March 29, 2021 20:39
Show Gist options
  • Save giovani-pereira-ifood/cfa2aa716ad731c1a6baadc7808fa62b to your computer and use it in GitHub Desktop.
Save giovani-pereira-ifood/cfa2aa716ad731c1a6baadc7808fa62b to your computer and use it in GitHub Desktop.
func createSnapshot(from icon: Icon) -> UIImage {
// Create view and add as subview on the window
let window: UIWindow = UIApplication.shared.keyWindow ?? UIWindow()
let iconView = IconView(icon: icon)
window.addSubview(iconView)
// Set a default size for all the snapshots
iconView.frame = CGRect(origin: .zero, size: dataImageStorageSize)
// Take a snapshot
let bounds = iconView.bounds
let renderer = UIGraphicsImageRenderer(bounds: bounds)
let image = renderer.image { _ in
iconView.drawHierarchy(in: bounds, afterScreenUpdates: true)
}
return image
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment