gpt4 book ai didi

swift - 从 UIImagePickerController 中选择的图像未显示在 UIImageView 中

转载 作者:行者123 更新时间:2023-11-30 12:14:04 25 4
gpt4 key购买 nike

我试图在持有 UIImageView 的 collectionViewCell 内显示 UIImagePickerController 中选定的图像。我可以打开并加载 imagePicker,但是当我选择图像时,UIImageView 仍然具有原始的占位符图像。我相信这与将 selectedImage 传递到实际单元格有关,而不是传递到登录 Controller 有关,但我不确定。预先感谢您的帮助!

//loginCell类LoginCell 类:UICollectionViewCell、UIImagePickerControllerDelegate、UINavigationControllerDelegate {

lazy var profileImageView: UIImageView = {
let imageView = UIImageView()
imageView.image = UIImage(named: "imagefile")
//...
imageView.addGestureRecognizer(UITapGestureRecognizer(target:self, action: #selector(handleSelectProfileImage)))
imageView.isUserInteractionEnabled = true
return imageView
}()

var loginController: LoginController?

func handleSelectProfileImage() {
guard let loginController = delegate as? LoginController else {
return
}
loginController.showImagePicker()
}

}

//登录 Controller 类类 LoginController:UIViewController、UICollectionViewDelegate、UICollectionViewDataSource、UICollectionViewDelegateFlowLayout、LoginControllerDelegate、UIImagePickerControllerDelegate、UINavigationControllerDelegate {

weak var loginCollectionView: UICollectionView!

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

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

var selectedImageFromImagePicker: UIImage?

if let editedImage = info["UIImagePickerControllerEditedImage"] as? UIImage {
selectedImageFromImagePicker = editedImage
} else if let originalImage = info["UIImagePickerControllerOriginalImage"] as? UIImage {
selectedImageFromImagePicker = originalImage
}

if let selectedImage = selectedImageFromImagePicker {
loginCell.profileImageView.image = selectedImage
}
dismiss(animated: true, completion: nil)
}

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let loginCell = collectionView.dequeueReusableCell(withReuseIdentifier: loginCellId, for: indexPath) as! LoginCell
loginCell.delegate = self
return loginCell
}

最佳答案

您将在此处创建 LoginCell 的新实例:

if let selectedImage = selectedImageFromImagePicker {
let loginCell = LoginCell()
loginCell.profileImageView.image = selectedImage
}

您应该删除第二行,以便您使用名为 loginCell 的类变量,而不是本地变量,根据您的代码,本地变量不会显示在任何地方。

if let selectedImage = selectedImageFromImagePicker {
loginCell.profileImageView.image = selectedImage
}

关于swift - 从 UIImagePickerController 中选择的图像未显示在 UIImageView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45642098/

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