gpt4 book ai didi

ios - Swift 从 UIView 层获取 UIImage

转载 作者:可可西里 更新时间:2023-11-01 00:56:42 24 4
gpt4 key购买 nike

我的 UIView 中有 CAShapeLayers,我想要 View 中的 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() 返回 nil 并且UIGraphicsGetImageFromCurrentImageContext() 也是如此。

关于ios - Swift 从 UIView 层获取 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45724163/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com