-1

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
ddfasdfasd
  • 11
  • 4

1 Answers1

3

If I understand your question, you want a true screen shot but not of the entire screen. Therefore, take a raw screenshot:

let snap = UIApplication.shared.keyWindow!.snapshotView(afterScreenUpdates: true)
snap.frame = view.bounds // or something, if necessary

Apply a mask to the screenshot with a frame equal to the bounds of the table view:

let mask = CALayer()
mask.frame = tableView.frame.bounds
mask.backgroundColor = UIColor.white.cgColor
snap.layer.mask = mask
trndjc
  • 11,654
  • 3
  • 38
  • 51