图片处理 之 截屏

2019-10-08  本文已影响0人  小凡凡520
// 开启上下文
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, true, 0)

// 渲染
self.view.drawHierarchy(in: self.view.bounds, afterScreenUpdates: true)

let result = UIGraphicsGetImageFromCurrentImageContext()

// 关闭上下文
UIGraphicsEndImageContext()

resultImgView.image = result
// 开启上下文
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, true, 0)

guard let context = UIGraphicsGetCurrentContext() else {
    return
}

self.view.layer.render(in: context)

guard let cgImg = context.makeImage() else {
    return
}

// 关闭上下文
UIGraphicsEndImageContext()

resultImgView.image = UIImage(cgImage: cgImg)
上一篇 下一篇

猜你喜欢

热点阅读