问题描述
我正在尝试使用KeyValueObserver或任何其他可能的方法来检测UIImageView(UIimage)是否已更改,我的图像视图在首次加载时具有图像,因此没有image.image != nil
我使用的是UIPickerController,所以当我选择图像时,值会改变,我想检测到该改变,因此我可以运行一些代码,例如 Ex:更改按钮的颜色或启用它 >
btn.backgroundColor = UIColor.green
btn.isEnabled = true
所以我尝试用这种方式添加观察者,但是什么也没发生,没有打印输出,没有断点
let driverPImg: UIImageView = {
let image = UIImageView(image: #imageLiteral(resourceName: "DriverPimg"))
image.layer.cornerRadius = 75
image.clipsToBounds = true
image.contentMode = .scaleAspectFill
image.isUserInteractionEnabled = true
image.translatesAutoresizingMaskIntoConstraints = false
return image
}()
let key1: String = "key1"
// in ViewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
self.ProfileImg.image!.addObserver(self,forKeyPath: key1,options: .new,context: nil)
}
override func observeValue(forKeyPath keyPath: String?,of object: Any?,change: [NSKeyValueChangeKey : Any]?,context: UnsafeMutableRawPointer?) {
if keyPath == key1{
print("not changed")
}else{
print("it did changed")
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)