【发布时间】:2017-08-02 04:23:06
【问题描述】:
我想将更改后的 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 添加动画代码。谢谢。
-
在添加动画之前试试这个
animation.removedOnCompletion = false。 -
@adev 我已经完成了(在“viewDidAppear”中)。并添加 viewdidAppear 代码
-
animateLayer是否在viewDidAppear中的该行之前调用?
标签: ios objective-c iphone uiview