I am trying to swizzle UIImage.init(named:) but the init is not being called
extension UIImage {
@objc public convenience init?(swizzledName: String) {
self.init(named: swizzledName)
/// Do something
print("this is working")
}
static func swizzle() {
guard let instance = class_getClassMethod(self, #selector(UIImage.init(named:))),
let swizzledInstance = class_getClassMethod(self, #selector(UIImage.init(swizzledName:))) else { return }
method_exchangeImplementations(instance, swizzledInstance)
}
}
Usage
UIImage.swizzle()
let image = UIImage(named: "avatar")
not working
I too was having trouble swizzling these
initmethods forUIImage. The only way I found was to instead use class methods, which seem to work fine (I first tried withstaticmethods, but that didn't work, so then I triedclassmethods, which did work):