坑录

2019-12-02  本文已影响0人  Codepgq

方法交换


extension UIImage {
    static func pq_exchangeImplementations() {
        guard let m1 = class_getInstanceMethod(self, #selector(self.init(named:))) else {
            return
        }
        guard let m2 = class_getInstanceMethod(self, #selector(swizzle_named(_:))) else {
            return
        }

        if (class_addMethod(self, #selector(swizzle_named(_:)), method_getImplementation(m2), method_getTypeEncoding(m2))) {
            class_replaceMethod(self, #selector(swizzle_named(_:)), method_getImplementation(m1), method_getTypeEncoding(m1))
        } else {
            method_exchangeImplementations(m1, m2)
        }
    }
    
    @objc func swizzle_named(_ named: String) {
        print("使用image(named: \(named)) 加载图片")
        if !(named.isEmpty) {
            swizzle_named(named)
            return
        }
        print("图片名字为空,不加载")
    }
}
上一篇下一篇

猜你喜欢

热点阅读