我想保存更改UIView作为UIImage。但是,它将始终保存为第一个(默认)视图。如何将UIView转换(编辑)为UIImage?
这是第一个(默认)UIView。
应用程序使用渐变动画来改变颜色。 的改变UIView的是一样的,如下图:
我写的代码如下:
extension UIImage {
convenience init(layer: CALayer, view: UIView) {
UIGraphicsBeginImageContext(view.frame.size)
layer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.init(cgImage: (image?.cgImage)!)
}
}
顺便说一句,图像保存与默认的UIView。
如何用更改的UIView保存图像?
[编辑]添加动画代码
func animateLayer(){
if index == 11 {
addEmitter(a: index, b: 0)
fromColors = [Colors.fromColorList[index], Colors.fromColorList[0]]
toColors = [Colors.toColorList[index], Colors.toColorList[0]]
index = 0
} else {
addEmitter(a: index, b: index + 1)
fromColors = [Colors.fromColorList[index], Colors.fromColorList[index+1]]
toColors = [Colors.toColorList[index], Colors.toColorList[index+1]]
index += 1
}
animation.fromValue = fromColors
animation.toValue = toColors
animation.duration = 1.00
mind.mindAnimation(animation: animation)
}
[EDIT2]添加ViewDidAppear
override func viewDidAppear(_ animated: Bool) {
animation.isRemovedOnCompletion = false
animation.fillMode = kCAFillModeForwards
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
animation.delegate = self
}
也为动画添加代码。你需要在那里改变。 – adev
@adev为动画添加代码。谢谢。 –
在向动画添加动画之前,尝试使用'animation.removedOnCompletion = false'。 – adev