如何根据swift库中的图像调整imageView的大小
我正在查看图像 容器视图约束如何根据swift库中的图像调整imageView的大小,swift,uiimageview,uiimagepickercontroller,Swift,Uiimageview,Uiimagepickercontroller,我正在查看图像 容器视图约束 top = 40, leading = 0, trailing = 0, height = 180 带有containerView的图像视图约束 top = 0, leading = 0, trailing = 0, bottom = 0 我正在从图库代码获取图像:我正在使用imagepicker获取图像 class FinalViewController: UIViewController, UIImagePickerControllerDeleg
top = 40, leading = 0, trailing = 0, height = 180
带有containerView的图像视图约束
top = 0, leading = 0, trailing = 0, bottom = 0
我正在从图库代码获取图像:我正在使用imagepicker获取图像
class FinalViewController: UIViewController, UIImagePickerControllerDelegate,UINavigationControllerDelegate {
@IBOutlet weak var profileImg: UIImageView!
@IBOutlet weak var picContainerView: UIView!
var picker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
picker.delegate = self
}
override func viewWillAppear(_ animated: Bool) {
self.navigationController?.navigationBar.isHidden=true
}
@IBAction func profileButton(_ sender: Any) {
let actionSheetController: UIAlertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
actionSheetController.popoverPresentationController?.sourceView = self.picContainerView
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .cancel) { action -> Void in
}
actionSheetController.addAction(cancelAction)
let takePictureAction: UIAlertAction = UIAlertAction(title: "TakePhoto", style: .default) { action -> Void in
self.openCameraPicker()
}
actionSheetController.addAction(takePictureAction)
let choosePictureAction: UIAlertAction = UIAlertAction(title: "ChooseFromLibrary", style: .default) { action -> Void in
self.openPhotoGallery()
}
actionSheetController.addAction(choosePictureAction)
//Present the
self.present(actionSheetController, animated: true, completion: nil)
}
func openCameraPicker() {
picker.sourceType = UIImagePickerController.SourceType.camera
picker.cameraCaptureMode = .photo
picker.allowsEditing = true
picker.modalPresentationStyle = .fullScreen
present(picker,animated: true,completion: nil)
}
func openPhotoGallery() {
picker.sourceType = .photoLibrary
picker.allowsEditing = true
picker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
present(picker, animated: true, completion: nil)
}
// MARK: - UIImagePickerControllerDelegate Methods
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let img = info[UIImagePickerController.InfoKey.editedImage] as? UIImage{
profileImg.image = img
}
else if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
self.profileImg.image = image
}
dismiss(animated: true, completion: nil)
}
}
如果我给出AspectFit
则o/p。。图像和空白
如果我给出AspectFill
则o/p。。图像拉伸
如何根据图像调整imageview的大小。请帮我输入代码首先尝试将ContainerView的背景设置为清晰,而不是白色。 如果这不起作用,请尝试以下方法:
UIImage *img = [UIImage imageNamed:@"logo.png"];
CGFloat width = img.size.width;
CGFloat height = img.size.height;
然后将ImageView的hight和width设置为ContainerView的width和hight。首先尝试将ContainerView的背景设置为clear,而不是白色。 如果这不起作用,请尝试以下方法:
UIImage *img = [UIImage imageNamed:@"logo.png"];
CGFloat width = img.size.width;
CGFloat height = img.size.height;
然后将ImageView的hight和width设置为ContainerViews width和hight。强制容器的大小,并强制ImageView为容器的大小。你希望如何调整?尝试将容器约束更改为高度≥ 100@claude31好吧,我当时的身高相当于。。但如果我给出了错误的约束条件,就不会有太大变化。。那么请务必让我知道。。我需要使图像适合imageView您强制容器的大小,您强制imageView为容器的大小。你希望如何调整?尝试将容器约束更改为高度≥ 100@claude31好吧,我当时的身高相当于。。但如果我给出了错误的约束条件,就不会有太大变化。。那么请务必让我知道。。我需要使图像适合imageView