[英]Convert UIView to UIImage Swift 3
我正在开发将UIView
转换为UIImage
的扩展程序,但是遇到一个奇怪的问题,我可以在iOS Simulator中获取正确的图像,但在真实设备中却得到黑色图像。 下面是我的代码
extension UIView {
func screenshotImage() -> UIImage {
UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, 0.0);
self.drawHierarchy(in: self.bounds, afterScreenUpdates: true)
let screenShot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return screenShot!
}
}
谁能解释我在真实设备中无法获得正确图像的错误做法?
编辑
观察结果:
每当我将UIView传递给模拟器中的此扩展程序时,我都会获得该视图的完美图像
每当我在真实设备中将UIView传递给此扩展时,我都会得到一张全黑的图像,而不是该UIView中的元素(与模拟器结果不同)。
extension UIImage {
class func imageWithView(view: UIView) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.isOpaque, 0.0)
view.drawHierarchy(in: view.bounds, afterScreenUpdates: true)
let img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return img
}
}
希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.