Swift 从 UIView 层获取 UIImage
Swift Get UIImage from layers of UIView
我的 UIView 中有 CAShapeLayers,我想要视图中的 UIImage。
所以我正在尝试通过下面的函数,但在
处出现错误
UIGraphicsGetCurrentContext()
extension UIView
{
func getImgFromVyuFnc() -> UIImage
{
UIGraphicsBeginImageContext(self.frame.size)
self.layer.renderInContext(UIGraphicsGetCurrentContext())
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
}
fatal error: unexpectedly found nil while unwrapping an Optional value
2017-08-17 04:17:47.230474 PicPac[1095:488887] fatal error: unexpectedly found nil while unwrapping an Optional value
如果传递给 UIGraphicsBeginImageContext(_)
的大小在一个或两个维度中为零,则 UIGraphicsGetCurrentContext()
returns nil
等 UIGraphicsGetImageFromCurrentImageContext()
。
我的 UIView 中有 CAShapeLayers,我想要视图中的 UIImage。
所以我正在尝试通过下面的函数,但在
处出现错误
UIGraphicsGetCurrentContext()
extension UIView
{
func getImgFromVyuFnc() -> UIImage
{
UIGraphicsBeginImageContext(self.frame.size)
self.layer.renderInContext(UIGraphicsGetCurrentContext())
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
}
fatal error: unexpectedly found nil while unwrapping an Optional value 2017-08-17 04:17:47.230474 PicPac[1095:488887] fatal error: unexpectedly found nil while unwrapping an Optional value
如果传递给 UIGraphicsBeginImageContext(_)
的大小在一个或两个维度中为零,则 UIGraphicsGetCurrentContext()
returns nil
等 UIGraphicsGetImageFromCurrentImageContext()
。