Gözde Bal
Gözde Bal

Reputation: 37

Image couldn't be seen in the Image View Area in Swift

I have an image view in my view controller but weirdly I can't show my image inside of the imageview. It is located in the middle of the page or somewhere else in the other view controllers. My code of both image view and image picker are below.

image view in the VC

And this is the output. Arrow image doesn' t show in expected area:

and this is the output. Arrow image doesn' t show in expected area

profileImageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
profileImageView.bottomAnchor.constraint(equalTo: nameTextField.topAnchor, constant: -12).isActive = true
profileImageView.widthAnchor.constraint(equalToConstant: 150).isActive = true
profileImageView.heightAnchor.constraint(equalToConstant: 150).isActive = true

profileImageView.image = UIImage(named: "arrow_long_left")
profileImageView.translatesAutoresizingMaskIntoConstraints = false
profileImageView.contentMode = .scaleAspectFill
profileImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleSelectProfileImageView)))
profileImageView.isUserInteractionEnabled = true
view.addSubview(profileImageView)   



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 {
            profileImageView.image = selectedImage
        }

        dismiss(animated: true, completion: nil)
    }

func handleSelectProfileImageView() {

        let imagePicker = UIImagePickerController()

        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) {
            imagePicker.delegate = self
            imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
            //picker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
            imagePicker.allowsEditing = true
            self.present(imagePicker, animated: true, completion: nil)
        }

        /*else if (UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
            let picker = UIImagePickerController()
            picker.delegate = self
            picker.sourceType = UIImagePickerControllerSourceType.camera;
            picker.allowsEditing = true
            present(picker, animated: true, completion: nil)

        }*/
    }
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        print("canceled picker")
        dismiss(animated: true, completion: nil)
    }

Upvotes: 0

Views: 65

Answers (1)

dmorrow
dmorrow

Reputation: 5694

Your code is off here:

profileImageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
profileImageView.bottomAnchor.constraint(equalTo: nameTextField.topAnchor, constant: -12).isActive = true
profileImageView.widthAnchor.constraint(equalToConstant: 150).isActive = true
profileImageView.heightAnchor.constraint(equalToConstant: 150).isActive = true

My guess is you want something more like this:

profileImageView.leadingAnchor.constraint(equalTo: nameTextField.trailingAnchor, constant:10).isActive = true
profileImageView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant:10).isActive = true
profileImageView.topAnchor.constraint(equalTo: nameTextField.topAnchor).isActive = true
profileImageView.bottomAnchor.constraint(equalTo: emailTextField.bottomAnchor).isActive = true

If you want to fix the width and height to 150, you'll need to adjust a bit. But, right now you're anchored to the centerX of the view, so its horizontally centered. And anchored by the bottom to the top of the nameTextField - so of course its above the nameTextField

Upvotes: 1

Related Questions