如何在Swift中为UIImage着色? [英] How can I color a UIImage in Swift?

查看:123
本文介绍了如何在Swift中为UIImage着色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张名为 arrowWhite 的图片。我想将此图像着色为黑色。

I have an image called arrowWhite. I want to colour this image to black.

func attachDropDownArrow() -> NSMutableAttributedString {
        let image:UIImage = UIImage(named: "arrowWhite.png")!
        let attachment = NSTextAttachment()
        attachment.image = image
        attachment.bounds = CGRectMake(2.25, 2, attachment.image!.size.width - 2.25, attachment.image!.size.height - 2.25)
        let attachmentString = NSAttributedString(attachment: attachment)
        let myString = NSMutableAttributedString(string: NSString(format: "%@", self.privacyOptions[selectedPickerRow]) as String)
        myString.appendAttributedString(attachmentString)
        return myString
    }

我想要在 blackColour 中获取此图像。

tintColor 无效......

I want to get this image in blackColour.
tintColor is not working...

推荐答案

有一个内置的方法来获取 UIImage ,它会自动呈现在< a href =https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/UIKitUICatalog/#//apple_ref/doc/uid/TP40012857-UIView-SW17>模板模式。这使用视图的tintColor为图像着色:

There's a built in method to obtain a UIImage that is automatically rendered in template mode. This uses a view's tintColor to color the image:

let templateImage = originalImage.imageWithRenderingMode(UIImageRenderingModeAlwaysTemplate)
myImageView.image = templateImage
myImageView.tintColor = UIColor.orangeColor()

这篇关于如何在Swift中为UIImage着色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆