UIImagePickerController 从 UICollectionViewCell 中的 UIImageView 呈现

UIImagePickerController presented from UIImageView inside UICollectionViewCell

我无法从 UIImageView 中的 UIImagePickerController 加载所选图像。 UIImageView 在点击 collectionViewCell 内的按钮时显示。在 collectionViewCell 中点击那个按钮,UIImageView 动画进入视图。点击那个 UIImageView 会显示 uiimagepicker。 The picker dismisses fine when an image is selected, but the image does not load inside the uiimageView on picker dismiss (the placeholder image remains and is not replaced by the selected image).我的控制台没有发生崩溃或错误。感谢您的帮助!

UICollectionViewCell class - LoginCell

class LoginCell: UICollectionViewCell, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

lazy var createUsernameButton: UIButton = {
    let customButton = UIButton(type: .system)
    customButton.setTitle("Create Username", for: .normal)
    customButton.addTarget(self, action: #selector(animateIn), for: .touchUpInside)
    return customButton
}()

func animateIn() {
    addSubview(profileImageView)

    profileImageView.transform = CGAffineTransform.init(scaleX: 1.3, y: 1.3)
    profileImageView.alpha = 0

    UIView.animate(withDuration: 0.2) {
        self.profileImageView.alpha = 1
        self.profileImageView.transform = CGAffineTransform.identity
    }

}

    lazy var profileImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "avatar-bg-2x")
        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()
    }

//.....
}

登录控制器 class

class LoginController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, LoginControllerDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    weak var loginCollectionView: UICollectionView!
    var loginCell: LoginCell?

    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
        }

        dismiss(animated: true, completion: {
            if let selectedImage = selectedImageFromImagePicker {
                self.loginCell?.profileImageView.image = selectedImage
                print("was dismissed")
            }
        })
    }

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

我发现了问题,您没有在主线程上更新图像。试试这个:

1. 更改登录控制器中的代码 class 以匹配下面的代码。

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 {
        DispatchQueue.main.async {
            self.loginCell?.profileImageView.image = selectedImage
            self.loginCollectionView.reloadData() //EDIT: add this new piece of code
        }
    }

    dismiss(animated: true, completion: nil)
}

var profileImage: UIImage?

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let loginCell = collectionView.dequeueReusableCell(withReuseIdentifier: loginCellId, for: indexPath) as! LoginCell
        loginCell.delegate = self
        loginCell.profileImageView.image = profileImage != nil ? profileImage : UIImage(named: "avatar-bg-2x")
        return loginCell
}

2. 在您的 loginCell class 中更改此代码以匹配以下代码。

lazy var profileImageView: UIImageView = {
    let imageView = UIImageView()
    //removed line here
    imageView.addGestureRecognizer(UITapGestureRecognizer(target:self, action: #selector(handleSelectProfileImage)))
    imageView.isUserInteractionEnabled = true
    return imageView
}()