简体   繁体   English

如何着色动画UIImageView?

[英]How to tint an animated UIImageView?

I'm attempting to apply tint of a UIImageView consisting of programatically loaded animationImages . 我正在尝试应用由编程加载的animationImages组成的UIImageView色调。 Currently using iOS 9.3. 目前使用的是iOS 9.3。

I think I've tried every proposed solution found here for applying the tint including: 我想我已经尝试了在这里找到的所有解决方案,用于应用色调,包括:

  • Setting the .renderMode on load with: let newImage: UIImage? = UIImage(named: filename as String)!.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate) 在加载时设置.renderMode: let newImage: UIImage? = UIImage(named: filename as String)!.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate) let newImage: UIImage? = UIImage(named: filename as String)!.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
  • Setting the tintView either in the Storyboard or programatically with: zeroImageView.tintColor = UIColor.redColor() 在Storyboard中设置tintView或以编程方式设置: zeroImageView.tintColor = UIColor.redColor()
  • Setting the image .renderMode through the UIImageView itself: zeroImageView.image = zeroImageView.image!.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate) 通过UIImageView本身设置图像.renderMode: zeroImageView.image = zeroImageView.image!.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
  • Setting the UIImageView's renderMode through the Interface Builder user defined runtime attributes: imageRenderingMode Number 2 通过Interface Builder用户定义的运行时属性设置UIImageView的renderMode: imageRenderingMode Number 2

The image sequence is of PNGs with transparency, so, I would like to re-colour the image and maintain the transparency. 图像序列是具有透明度的PNG,因此,我想重新着色图像并保持透明度。

I've had no luck and am sort of at a loss. 我没有运气,有点不知所措。 Wondering if maybe these methods only work for a still UIAnimationView? 想知道这些方法是否仅适用于静态UIAnimationView? Any help would be greatly appreciated, thanks! 任何帮助将不胜感激,谢谢!

Here's some code: 这是一些代码:

// Load all images
    while let element = enumerator.nextObject() as? String {
        if element.hasSuffix("png") {
            let filename: NSString = "Digits/0/" + element
            print(filename)
            imageNames.append(filename as String)
            let newImage: UIImage? = UIImage(named: filename as String)!.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
            images.append(newImage!)
            zeroImageView.tintColor = UIColor.redColor()

        }
    }

// Make animated UIImageView
    zeroImageView.userInteractionEnabled = false
    zeroImageView.animationImages = images
    zeroImageView.animationDuration = 2.0
    zeroImageView.startAnimating()
    zeroImageView.tintColor = UIColor.redColor()

If none of those worked the next step I would do is programmatically place an empty UIView on top of the UIImageView. 如果下一步我没做的就是以编程方式在UIImageView上放置一个空的UIView。 First off, it would not interfere with anything going on in the UIImageView. 首先,它不会干扰UIImageView中发生的任何事情。

I would check out this tutorial here: http://www.apptuitions.com/programmatically-creating-uiview-uislider-uiswitch-using-swift/ 我会在这里查看这个教程: http//www.apptuitions.com/programmatically-creating-uiview-uislider-uiswitch-using-swift/

this will teach you how to programmatically add a UIView to the storyboard. 这将教你如何以编程方式将UIView添加到故事板。 I would add this code in the viewDidLoad() method. 我会在viewDidLoad()方法中添加此代码。

The main thing you need to change is the background color of the UIView. 你需要改变的主要是UIView的背景颜色。 First you have to decide what the color of the tint (which from what I see in your code is red) and then explicitly change the 'Alpha' of the color so that it is barely showing. 首先,你必须决定色调的颜色(从我在你的代码中看到的是红色),然后明确地改变颜色的'Alpha',使它几乎不显示。 You change the 'Alpha' because that is basically the opacity of the color. 您更改'Alpha',因为这基本上是颜色的不透明度。

Hope this helps. 希望这可以帮助。

Try this... 尝试这个...

extension UIImage {
    func image(withTint tint: UIColor) -> UIImage? {
        guard let cgImage = cgImage else {
            return nil
        }

        UIGraphicsBeginImageContextWithOptions(size, false, scale)
        guard let context = UIGraphicsGetCurrentContext() else {
            return nil
        }

        let rect = CGRect(origin: .zero, size: size)

        context.translateBy(x: 0, y: size.height)
        context.scaleBy(x: 1.0, y: -1.0)
        context.setBlendMode(.normal)
        context.clip(to: rect, mask: cgImage)
        tint.setFill()
        context.fill(rect)

        let image = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return image
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM