0

When I came across most of the examples, I notice almost all of them are using withRenderingMode, when assigning an UIImage to UIImageView, UIButton, ...

Examples are :-

Action Sheet with Image for each option on iOS

Is there any way to use "withRenderingMode(.alwaysTemplate)" with default image by not adding any tint colour of a button?

My initial thoughts is that, the purpose of using withRenderingMode, is to generate a new instance of UIImage, so that changing the tint color of an UI component, will not affect rest of others.

I wrote a simple program without using withRenderingMode

class ViewController: UIViewController {
    
    @IBOutlet weak var imageView1: UIImageView!
    
    @IBOutlet weak var imageView2: UIImageView!

    @IBOutlet weak var imageView3: UIImageView!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        let chooseImage = UIImage(systemName: "photo")
        
        imageView1.image = chooseImage
        
        imageView2.image = chooseImage
        
        imageView3.image = chooseImage
        
        imageView1.tintColor = .red
        
        imageView2.tintColor = .green
    }

I expect all UIImageView will end up of having same image color, but it doesn't happen.

enter image description here

Can anyone show an example, on when using withRenderingMode is really necessary?

Thanks.

Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875

1 Answers1

0

WithRenderingMode doesn't change the image at all or make a new image. It just sets how the image will appear when used in a UIImageView. The docs say "The rendering mode controls how UIKit uses color information to display an image" and the idea of a rendering mode was created because in iOS 7 there was this new visual style where images and text in the "tint color" of the app are considered to be tap-able buttons. And often you want an image to be like this, a 'template' image, and be rendered in the tintColor of the UIView that it's in. The alpha channel (transparency) is used for background, anything not alpha is foreground, if the rendering mode is .alwaysTemplate

Shadowrun
  • 3,572
  • 1
  • 15
  • 13