如何根据图库中的图像快速调整imageView的大小

问题描述

我正在将imageView放置在视图中。

containerView约束

   top = 40,leading = 0,trailing = 0,height = 180

带有containerView的imageView约束

  top = 0,bottom = 0 

我正在从图库代码中获取图像:我正在使用图像选择器获取图像

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,completion: nil)
        }
        func openPhotoGallery() {
            picker.sourceType = .photoLibrary
            picker.allowsEditing = true

            picker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
            present(picker,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 ..图像以及空白

enter image description here

如果我给AspectFill ,则o / p ..图片会拉伸

enter image description here

如何根据图像调整imageview的大小。请帮助我提供代码

解决方法

首先尝试将ContainerView的背景设置为清晰而不是白色。 如果这不起作用,请尝试以下操作:

UIImage *img = [UIImage imageNamed:@"logo.png"];

CGFloat width = img.size.width;
CGFloat height = img.size.height;

然后将ImageView的高度和宽度设置为ContainerViews的宽度和高度。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...