【发布时间】:2018-05-14 18:14:08
【问题描述】:
我需要做一个tableview的截图(它有一个清晰的背景)。但在它下面,我也有一个 ImageView 和模糊视图。我需要获取所有这些视图的屏幕截图。
我尝试了不同的解决方案,但它们没有捕捉到底部视图。有什么办法吗?
另外,我不需要完整的屏幕截图。只有受 tableView 限制的边界。我想,我有一些选择如何制作完整的屏幕截图,但它不适用于视图
我试过了:
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()
我的层次结构是:
-
基础 UIView
-
带有图像的 UIImageView
-
UIBlurEffect
- 具有清晰背景的 UITableView 显示图表
-
-
【问题讨论】:
-
您能否发布屏幕截图以便我们更好地理解问题?
-
您应该将所有这些视图放在新的 uiview 上并截取此视图。它看起来像这样 UIViewController - NewView - (所有你需要的视图)
-
另外,您能否发布更多内容(例如代码),因为我们真的不知道您尝试过什么“不同的解决方案”?
-
@Jeremy Sh 我更新了我的问题。
-
如果没有屏幕截图,很难想象你想做什么
标签: ios swift uiview calayer screen-capture