Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将UIView转换为UIImage Swift 3_Ios_Iphone_Swift3_Uiview_Uiimage - Fatal编程技术网

Ios 将UIView转换为UIImage Swift 3

Ios 将UIView转换为UIImage Swift 3,ios,iphone,swift3,uiview,uiimage,Ios,Iphone,Swift3,Uiview,Uiimage,我正在开发一个将UIView转换为UIImage的扩展,但我面临一个奇怪的问题,即我能够在iOS模拟器中获得正确的图像,但在实际设备中获得黑色图像。下面是我的代码 extension UIView { func screenshotImage() -> UIImage { UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, 0.0); self.drawHierarchy(in: self.boun

我正在开发一个将UIView转换为UIImage的扩展,但我面临一个奇怪的问题,即我能够在iOS模拟器中获得正确的图像,但在实际设备中获得黑色图像。下面是我的代码

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
    }
}

希望这有帮助

请检查,我想你已经得到了答案。具体告诉我们你的代码是如何无法完成你希望它完成的任务的。你犯了什么错误,或者结果怎么不是你想要的?@duncac我编辑了我的问题并添加了我的观察结果。让我知道,如果它仍然是不明确的嗨,谢谢你的回答,我已经尝试过了&只是再次验证了它&我也得到了同样的东西。它在模拟器上运行良好,但在真实设备上返回黑色图像。如果您得到黑色图像,您可能希望尝试将不透明设置为false:UIGraphicsBeginImageContextWithOptionsview.bounds.size,false,0.0