0
我在視圖控制器中有一個圖像視圖,但很奇怪,我無法在imageview內部顯示圖像。它位於頁面的中間或其他視圖控制器中的其他位置。我的圖像視圖和圖像選擇器的代碼如下。無法在Swift中的圖像視圖區域中看到圖像
這是輸出。箭頭圖像沒有按」 T顯示在預期範圍:
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)
}
非常感謝dmorrow,它非常棒!但正如你所說當我試圖安排寬度和高度與「profileImageView.widthAnchor.constraint(equalToConstant:80).isActive = true profileImageView.heightAnchor.constraint(equalToConstant:80).isActive = true」代碼塊,它沒有'工作。我該怎麼做,你有什麼建議嗎? – gozdebal
@gozdebal如果您要發送圖像的高度/寬度,則不應設置尾部或底部錨點,因爲這會限制圖像。這完全取決於您希望佈局如何響應不同大小的設備 – dmorrow