ios - bar - uinavigationitem title color
Cómo cambiar el color de un UIImage (5)
No quiero cambiar el backgroundColor de un UIImage,
sino más bien para cambiar el color de la imagen completa.
Porque tengo formularios estándar que puedo mover de popover a la vista.
En mi aplicación, es posible manipular las imágenes en una imagen grande, es decir, una tabla.
Además quiero cambiar el color a marrón blanco o qué más.
Pero el problema es: solo puedo cambiar el backgroundColor
¿Hay alguna manera de cambiar el color del UIImage?
Gracias de antemano
UIImage *image = [UIImage imageNamed:@"triangle.png"];
CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClipToMask(context, rect, image.CGImage);
CGContextSetFillColorWithColor(context, [[UIColor redColor] CGColor]);
CGContextFillRect(context, rect);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImage *flippedImage = [UIImage imageWithCGImage:img.CGImage
scale:1.0 orientation: UIImageOrientationDownMirrored];
yourUIImageView.image = flippedImage;
También puede hacer esto rápidamente con el siguiente código:
// language: Swift
let tintedImage = UIImageView(image: UIImage(named:"whatever")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate))
tintedImage.tintColor = UIColor.redColor()
Swift 3 + Interface Builder (guión gráfico)
Si agregó el UIImageView en el constructor de interfaz:
myIcon.image = myIcon.image!.withRenderingMode(UIImageRenderingMode.alwaysTemplate)
myIcon.tintColor = UIColor.red // your color
donde myIcon
es una salida de su guión gráfico, por ejemplo: @IBOutlet weak var myIcon: UIImageView!
Swift 3, xcode 8.1
Camino 1. Extensión UIImage
fuente: https://.com/a/40177870/4488252
extension UIImage {
convenience init?(imageName: String) {
self.init(named: imageName)!
accessibilityIdentifier = imageName
}
// https://.com/a/40177870/4488252
func imageWithColor (newColor: UIColor?) -> UIImage? {
if let newColor = newColor {
UIGraphicsBeginImageContextWithOptions(size, false, scale)
let context = UIGraphicsGetCurrentContext()!
context.translateBy(x: 0, y: size.height)
context.scaleBy(x: 1.0, y: -1.0)
context.setBlendMode(.normal)
let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
context.clip(to: rect, mask: cgImage!)
newColor.setFill()
context.fill(rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
newImage.accessibilityIdentifier = accessibilityIdentifier
return newImage
}
if let accessibilityIdentifier = accessibilityIdentifier {
return UIImage(imageName: accessibilityIdentifier)
}
return self
}
}
Uso
let imageView = UIImageView(frame: CGRect(x: 40, y: 250, width: 40, height: 40))
view.addSubview(imageView)
// Set color
imageView.image = UIImage(imageName: "Apple")?.imageWithColor(newColor: UIColor.blue)
// Reset color
//imageView.image = imageView.image?.imageWithColor(newColor: nil)
Camino 2. Extensión UIImageView
extension UIImageView {
var imageColor: UIColor? {
set (newValue) {
if let image = image {
if newValue != nil {
self.image = image.withRenderingMode(.alwaysTemplate)
tintColor = newValue
} else {
self.image = image.withRenderingMode(.alwaysOriginal)
tintColor = UIColor.clear
}
}
}
get {
return tintColor
}
}
}
Uso
let imageView = UIImageView(frame: CGRect(x: 40, y: 250, width: 40, height: 40))
view.addSubview(imageView)
imageView.image = UIImage(imageName: "Apple")
// Set color
imageView.imageColor = UIColor.green
// Reset color
//imageView.imageColor = nil
Muestra completa
import UIKit
class ImageView: UIImageView {
enum ImageColorTransformType {
case None, ImageExtension, ImageViewExtension
}
var imageColorTransformType = ImageColorTransformType.None
}
class ViewController: UIViewController {
@IBOutlet weak var colorSwitcher: UIButton!
var imageViews = [ImageView]()
private var appleImage: UIImage {
return UIImage(imageName: "Apple")!
}
private var redAppleImage: UIImage {
return UIImage(imageName: "RedApple")!
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
createNewImageView(x: 40, y:100, image:appleImage, imageColorTransformType: .None)
createNewImageView(x: 100, y:100, image:appleImage, imageColorTransformType: .ImageExtension)
createNewImageView(x: 160, y:100, image:appleImage, imageColorTransformType: .ImageViewExtension)
createNewImageView(x: 40, y:160, image:redAppleImage, imageColorTransformType: .None)
createNewImageView(x: 100, y:160, image:redAppleImage, imageColorTransformType: .ImageExtension)
createNewImageView(x: 160, y:160, image:redAppleImage, imageColorTransformType: .ImageViewExtension)
useOriginalColors = false
}
private func createNewImageView(x:CGFloat, y: CGFloat, image: UIImage, imageColorTransformType: ImageView.ImageColorTransformType) {
let imageView = ImageView(frame: CGRect(x: x, y: y, width: 40, height: 40))
imageView.image = image
imageView.imageColorTransformType = imageColorTransformType
imageViews.append(imageView)
view.addSubview(imageView)
}
private var _useOriginalColors = false
private var useOriginalColors: Bool {
set(value) {
_useOriginalColors = value
if (!value) {
colorSwitcher.setTitle("Original", for: .normal)
for imageView in imageViews {
switch imageView.imageColorTransformType {
case .ImageExtension:
imageView.image = imageView.image?.imageWithColor(newColor: UIColor.blue)
case .ImageViewExtension:
imageView.imageColor = UIColor.green
case .None:
break
}
}
} else {
colorSwitcher.setTitle("Template", for: .normal)
for imageView in imageViews {
switch imageView.imageColorTransformType {
case .ImageExtension:
imageView.image = imageView.image?.imageWithColor(newColor: nil)
case .ImageViewExtension:
imageView.imageColor = nil
case .None:
break
}
}
}
}
get {
return _useOriginalColors
}
}
@IBAction func switchColors(_ sender: Any) {
useOriginalColors = !useOriginalColors
}
}
extension UIImage {
convenience init?(imageName: String) {
self.init(named: imageName)!
accessibilityIdentifier = imageName
}
// https://.com/a/40177870/4488252
func imageWithColor (newColor: UIColor?) -> UIImage? {
if let newColor = newColor {
UIGraphicsBeginImageContextWithOptions(size, false, scale)
let context = UIGraphicsGetCurrentContext()!
context.translateBy(x: 0, y: size.height)
context.scaleBy(x: 1.0, y: -1.0)
context.setBlendMode(.normal)
let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
context.clip(to: rect, mask: cgImage!)
newColor.setFill()
context.fill(rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
newImage.accessibilityIdentifier = accessibilityIdentifier
return newImage
}
if let accessibilityIdentifier = accessibilityIdentifier {
return UIImage(imageName: accessibilityIdentifier)
}
return self
}
}
extension UIImageView {
var imageColor: UIColor? {
set (newValue) {
if let image = image {
if newValue != nil {
self.image = image.withRenderingMode(.alwaysTemplate)
tintColor = newValue
} else {
self.image = image.withRenderingMode(.alwaysOriginal)
tintColor = UIColor.clear
}
}
}
get {
return tintColor
}
}
}
Storyboard
Resultado
Detalles
Swift 3, xcode 8.1
La respuesta aceptada es correcta, pero hay una manera mucho más fácil para UIImageView
:
Obj-C:
UIImage *image = [UIImage imageNamed:@"foo.png"];
theImageView.image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[theImageView setTintColor:[UIColor redColor]];
Swift 2:
let theImageView = UIImageView(image: UIImage(named:"foo")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate))
theImageView.tintColor = UIColor.redColor()
Swift 3:
let theImageView = UIImageView(image: UIImage(named:"foo")!.withRenderingMode(.alwaysTemplate))
theImageView.tintColor = UIColor.red