I need to make a screenshot of tableview(it has a clear background). But under it, I have one ImageView and blur view as well. I need to get screenshot with all those views.
I tried different solutions, but they did not capture the bottom views. Any ways to do that?
Also, I do not need a full screenshot of the screen. Only bounds which are limited by tableView. I guess, I had some option how to make a full screenshot, but it did not work with view
I tried:
1:
func snapshot(of rect: CGRect? = nil) -> UIImage? {
// snapshot entire view
UIGraphicsBeginImageContextWithOptions(bounds.size, isOpaque, 0)
drawHierarchy(in: bounds, afterScreenUpdates: true)
let wholeImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
// if no `rect` provided, return image of whole view
guard let image = wholeImage, let rect = rect else { return wholeImage }
// otherwise, grab specified `rect` of image
let scale = image.scale
let scaledRect = CGRect(x: rect.origin.x * scale, y: rect.origin.y * scale, width: rect.size.width * scale, height: rect.size.height * scale)
guard let cgImage = image.cgImage?.cropping(to: scaledRect) else { return nil }
return UIImage(cgImage: cgImage, scale: scale, orientation: .up)
}
2:
UIGraphicsBeginImageContext(tableView.frame.size)
tableView.layer.render(in: UIGraphicsGetCurrentContext()!)
let image1 = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
My hierarchy is:
base UIView
UIImageView with image
UIBlurEffect
- UITableView with clear background which displays chart