imageview 未相应更新
Posted
技术标签:
【中文标题】imageview 未相应更新【英文标题】:imageview not updated accordingly 【发布时间】:2018-12-09 03:37:46 【问题描述】:我想创建一个带有从照片库中选择的照片的图像视图,并通过按下按钮在滚动视图中显示。但是选择后图像不显示,而仅在我再次按下按钮时才显示。有人可以帮我解决这个问题吗?
@IBAction func imageBtnPressed(_ sender: Any)
imagePicker()
let imageView = UIImageView(frame: CGRect(x: (scrollView.bounds.size.width-200)/2, y:(scrollView.bounds.size.height-100)/2, width: 200, height: 100))
imageView.image = imageOnCanvas
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
scrollView.addSubview(imageView)
func imagePicker()
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
imagePicker.allowsEditing = false
present(imagePicker, animated: true, completion: nil)
func imagePickerControllerDidCancel(_ picker: UIImagePickerController)
dismiss(animated: true, completion: nil)
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])
let selectedImage = info[.originalImage] as? UIImage
imageOnCanvas = selectedImage
dismiss(animated: true, completion: nil)
【问题讨论】:
【参考方案1】:您的代码不会因为 UIImagePickerController 出现而暂停。那么,让我们看看你的代码:
@IBAction func imageBtnPressed(_ sender: Any)
imagePicker()
let imageView = UIImageView(frame: CGRect(x: (scrollView.bounds.size.width-200)/2, y:(scrollView.bounds.size.height-100)/2, width: 200, height: 100))
imageView.image = imageOnCanvas
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
scrollView.addSubview(imageView)
func imagePicker()
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
imagePicker.allowsEditing = false
present(imagePicker, animated: true, completion: nil)
这正是就像说(用imagePicker
函数的内容代替函数调用):
@IBAction func imageBtnPressed(_ sender: Any)
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
imagePicker.allowsEditing = false
present(imagePicker, animated: true, completion: nil)
let imageView = UIImageView(frame: CGRect(x: (scrollView.bounds.size.width-200)/2, y:(scrollView.bounds.size.height-100)/2, width: 200, height: 100))
imageView.image = imageOnCanvas
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
scrollView.addSubview(imageView)
嗯,你可以清楚地看到,你指的是imageOnCanvas
,它还没有被设置。 imagePicker
仍在显示;用户尚未选择照片。但是你的代码已经完全结束了。您继续添加了一个子视图,该子视图由包含 imageOnCanvas
中的图像的图像视图组成在代码开始之前。
【讨论】:
确实如此。基本上,当您得到用户选择的照片 时,需要将更新滚动视图的调用移至didFinishPickingMediaWithInfo
。在那之前,没有什么可以更新的。以上是关于imageview 未相应更新的主要内容,如果未能解决你的问题,请参考以下文章
Pyqtgraph ImageView 在 QSlider 值更改后未更新
使用 Alamofireimage 从服务器获取图像后,Imageview 未更新