2

Description:

I am writing an extension for UIImage which should initialize a system image with a given custom type of SystemImage.

Everything works well except that the image tint color is not as I have configured in Storyboard for the UIImageView tint color.

This only occurs when using the custom init and calling self.init(cgImage: image.cgImage!).

When I access the image directly (UIImage.SystemImage.bolt_circle_fill.image) I get the image with the correct tint color.

  • Image is showing correctly - UIImage.SystemImage.bolt_circle_fill.image

  • Image is showing incorrectly with a Black tint color - UIImage(systemImage: .b_circle_fill)

Any assistance will be appreciated.

Code:

import UIKit

extension UIImage {
    
    convenience init(systemImage: SystemImage) {
        
        let image = systemImage.image
        
        self.init(cgImage: image.cgImage!)
    }
    
    enum SystemImage: String {
        
        case bolt_circle_fill
        case b_circle_fill
        case icloud_circle_fill
        case flag_circle_fill
        case play_circle_fill
        case heart_circle_fill
        case f_cursive_circle_fill
        
        var image: UIImage {
            
            let imageName = self.rawValue.replacingOccurrences(of: "_", with: ".")
            let image = UIImage(systemName: imageName) ?? UIImage()
            
            return image
        }
    }
}
Community
  • 1
  • 1
shbedev
  • 1,875
  • 17
  • 28
  • 1
    I would try refactoring your code so that you get a "systemName" from the enum and you call `self.init(systemName:)` in your convenience initializer. – rmaddy Nov 24 '19 at 16:49

0 Answers0