简体   繁体   中英

Swift Get UIImage from layers of UIView

I have CAShapeLayers in my UIView and I want UIImage from the view.
So I am trying thru below function but getting an error at
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()返回nil ,因此将UIGraphicsGetImageFromCurrentImageContext()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM