标题: iOS_给View的部门区域截图 snapshot for view [打印本页] 作者: 自由的羽毛 时间: 2024-8-5 09:36 标题: iOS_给View的部门区域截图 snapshot for view 1.将整个view截图返回image:
这些 api 已被废弃,所以需要判断 iOS 版本 写两套代码:
Replace usage of UIGraphicsBeginImageContextWithOptions with UIGraphicsImageRenderer.
Replace usage of UIGraphicsGetImageFromCurrentImageContext with UIGraphicsImageRendererContext.currentImage.
Swift 版本:
/// 截图整个view
/// - Returns: image
func mooSnapshot() -> UIImage? {
if self.window == nil {
return nil
}
let scale = UIScreen.main.scale
var image: UIImage? = nil
// 1. 创建绘图渲染格式
if #available(iOS 10.0, *) {
let format = UIGraphicsImageRendererFormat()
format.scale = scale
format.opaque = false
// 2. 创建绘图渲染器
let renderer = UIGraphicsImageRenderer(size: self.bounds.size,
format: format)
// 3. 绘制图
image = renderer.image { context in
let success = self.drawHierarchy(in: self.bounds, afterScreenUpdates: true)
let success = self.drawHierarchy(in: self.bounds, afterScreenUpdates: true)
print("draw success: \(success)")
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndPDFContext()
return image
}
复制代码
Replace usage of UIGraphicsBeginImageContext with UIGraphicsImageRenderer.
Replace usage of UIGraphicsBeginImageContextWithOptions with UIGraphicsImageRenderer.
Replace usage of UIGraphicsGetImageFromCurrentImageContext with UIGraphicsImageRendererContext.currentImage.
UIGraphicsEndImageContext should only be used alongside UIGraphicsBeginImageContext[WithOptions].
这些旧的 api 已经被废弃了,用文章开头的 api 取代 4.Tips