How can I change image tintColor
Try to generate new image for yourself
UIImage *newImage = [_sketchImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];UIGraphicsBeginImageContextWithOptions(image.size, NO, newImage.scale);[yourTintColor set];[newImage drawInRect:CGRectMake(0, 0, image.size.width, newImage.size.height)];newImage = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();_sketchImageView.image = newImage;
And use it.
Good luck
======= UPDATE =======
This solution will only change color of all pixel's image.
Example: we have a book image: http://pngimg.com/upload/book_PNG2113.png
And after running above code (exp: TintColor
is RED). We have:
SO: how your image is depends on how you designed it
In Swift you can use this extension: [Based on @VietHung's objective-c solution]
Swift 5:
extension UIImage { func imageWithColor(color: UIColor) -> UIImage? { var image = withRenderingMode(.alwaysTemplate) UIGraphicsBeginImageContextWithOptions(size, false, scale) color.set() image.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height)) image = UIGraphicsGetImageFromCurrentImageContext()! UIGraphicsEndImageContext() return image }}
Previous Swift version:
extension UIImage { func imageWithColor(color: UIColor) -> UIImage? { var image = imageWithRenderingMode(.AlwaysTemplate) UIGraphicsBeginImageContextWithOptions(size, false, scale) color.set() image.drawInRect(CGRect(x: 0, y: 0, width: size.width, height: size.height)) image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image }}
In swift 2.0 you can use this
let image = UIImage(named:"your image name")?.imageWithRenderingMode(.AlwaysTemplate)let yourimageView.tintColor = UIColor.redColor()yourimageView.image = image
In swift 3.0 you can use this
let image = UIImage(named:"your image name")?.withRenderingMode(.alwaysTemplate)let yourimageView.tintColor = UIColor.redyourimageView.image = image