微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何根据图库中的图像快速调整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的宽度和高度。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。