我的视图控制器中有一个图像视图,但奇怪的是我无法在imageview中显示我的图像。它位于页面中间或其他视图控制器中的其他位置。我的图像视图和图像选择器的代码如下。
这是输出。箭头图像未显示在预期区域中:
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)
}
答案 0 :(得分:1)
您的代码不在此处:
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.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
如果要将宽度和高度固定为150,则需要稍微调整一下。但是,现在你已经锚定到视图的centerX,所以它的水平居中。并且由nameTextField的底部锚定到顶部 - 所以当然它在nameTextField上面