【问题标题】:How to resize imageView according to images from gallery in swift如何根据图库中的图像快速调整imageView的大小
【发布时间】:2020-12-07 19:08:09
【问题描述】:

我正在将 imageView 放在视图中..

containerView 约束

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

带有 containerView 的 imageView 约束

  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 是容器的大小。你希望如何调整?尝试将容器约束更改为高度≥100
  • @claude31 好吧,我的身高已经达到或等于.. 但变化不大
  • 如果我给了错误的约束..那么请告诉我..我需要使图像适合 imageView

标签: swift uiimageview uiimagepickercontroller


【解决方案1】:

首先尝试将 ContainerView 的背景设置为清除而不是白色。 如果这不起作用,试试这个:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-18
    • 1970-01-01
    • 2011-07-19
    • 2015-03-09
    • 1970-01-01
    • 2013-02-02
    • 1970-01-01
    相关资源
    最近更新 更多