我的UIView中有CAShapeLayers,我想從視圖中獲取UIImage。
所以我想直通下面的功能,但在
UIGraphicsGetCurrentContext()Swift從UIView的圖層獲取UIImage
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
最可能的問題是'self.frame.size'在一個或兩個維度中爲零。 – idz
很高興我能夠提供幫助。現在發佈爲答案。祝你的程序好運! – idz