gpt4 book ai didi

ios - ImageView 不会更新新图像

转载 作者:可可西里 更新时间:2023-11-01 03:07:58 25 4
gpt4 key购买 nike

我正在尝试从用户的手机中选择一张图片,并使用该图片更新图片 View 。

我遇到的问题是它不会更新图像。它确实到达那里,因为我添加了一个断点并且它击中了断点,并且 ImageView 可以更改图像,因为我在其他地方手动尝试更改它。

但是当我需要它时它拒绝工作。

我的 ImageView :

    lazy var profilePictureImageView: UIImageView = {
let imageView = UIImageView()
imageView.image = UIImage(named: "DefaultUser")
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.contentMode = .scaleAspectFill

imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleSelectProfilePicture)))
imageView.isUserInteractionEnabled = true
return imageView
}()

通过在设备上选择照片来更新图像的代码:

import UIKit

extension ProfileVC: UIImagePickerControllerDelegate, UINavigationControllerDelegate {

func handleSelectProfilePicture() {
let picker = UIImagePickerController()
picker.delegate = self
picker.allowsEditing = true
present(picker, animated: true, completion: nil)
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
var selectedImageFromPicker: UIImage?
if let editedImage = info["UIImagePickerControllerEditedImage"] as? UIImage {
selectedImageFromPicker = editedImage
}
else if let originalImage = info["UIImagePickerControllerOriginalImage"] as? UIImage {
selectedImageFromPicker = originalImage
}
if let selectedImage = selectedImageFromPicker {
profilePictureImageView.image = selectedImage
}
dismiss(animated: true, completion: nil)
}

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
}

[编辑] 我发现的一件事是,如果我在具有不同 ImageView 的不同 VC 上尝试它,它会按预期工作。我能看到的两个 VC 之间唯一的主要区别是我希望它在上面工作但不会同时扩展“UIPickerViewDelegate”和“UIPickerViewDataSource”的那个。会不会有冲突?

最佳答案

试试这个,它对我有用:

DispatchQueue.main.async {
profilePictureImageView.image = croppedImage
profilePictureImageView.setNeedsDisplay()
}

本质上,您需要指定要通过以下方式更新图像

profilePictureImageView.setNeedsDisplay()

关于ios - ImageView 不会更新新图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42703795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com