0

how I can do new init like a

Attention: I need init, not static func

extension UIImage {

    convenience init?(named: String, in bundle: Bundle? = nil, compatibleWith collection: UITraitCollection? = nil, rendring: UIImageRenderingMode) {
        self.init(named: named, in: bundle, compatibleWith: collection)
        self = self.imageWithRenderingMode(rendring) // error
        return
    }

}

but I have error for it implementation

Error: Cannot assign to value: 'self' is immutable

EvGeniy Ilyin
  • 1,817
  • 1
  • 21
  • 38

1 Answers1

1

You can use something like this:

I wrote it in Swift 4.1, you can make required changes.

extension UIImage {
    convenience init?(named: String, in bundle: Bundle? = nil, compatibleWith collection: UITraitCollection? = nil, rendring: UIImageRenderingMode) {
        if let image = UIImage(named: named, in: bundle, compatibleWith: collection) {
            if let cgImage = image.withRenderingMode(rendring).cgImage {
                self.init(cgImage: cgImage)
            }
        }
        return nil
    }
}
Mukesh
  • 2,792
  • 15
  • 32