【问题标题】:How to convert (edited)UIView to UIImage?如何将(编辑的)UIView 转换为 UIImage?
【发布时间】: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


【解决方案1】:

要在动画期间获取动画层的内容,您应该使用表示层而不是视图层。对于您的代码,这应该是这样的:

if let layer = view.layer.presentation() {
    let image = UIImage(layer: layer, view: view)
    ...
}

您应该在动画完成后执行此代码,然后再将其从视图层中移除。

如果您不想反转动画,“反转”它的过程应该更容易。这意味着您可以交换开始颜色和结束颜色并直接在视图中设置目标颜色。在这种情况下,您不需要在动画结束时保留动画,您可以使用视图的图层来保存图像。

【讨论】:

    猜你喜欢
    • 2011-07-25
    • 2018-01-09
    • 2012-12-25
    • 1970-01-01
    • 2016-12-10
    • 1970-01-01
    • 2016-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多