【问题标题】:ios UIImage going outside of UIImageView Borderios UIImage 超出 UIImageView 边框
【发布时间】:2018-12-01 14:32:45
【问题描述】:

这里黑色边框显示 UIImageView 的父 UIView 和红色边框显示 UIImageView 我正在从服务器下载图像,但图像超出了 UIImageView 区域,如图所示。我正在以编程方式进行任何帮助,将不胜感激。我在下面添加代码块

let bottomView : UIView = UIView(frame: CGRect(x : 10, y: stackView.height, width: view.width * 0.75, height: view.width * 0.75 ))

view.addSubview(bottomView)

bottomView.layer.borderColor = UIColor.black.cgColor
bottomView.layer.borderWidth = 1

bottomView.translatesAutoresizingMaskIntoConstraints = false
bottomView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
bottomView.topAnchor.constraint(equalTo: stackView.bottomAnchor, constant: -20).isActive = true
bottomView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10).isActive = true
bottomView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10).isActive = true
bottomView.widthAnchor.constraint(equalToConstant: view.width * 0.75).isActive = true
bottomView.heightAnchor.constraint(equalToConstant: view.width * 0.75).isActive = true

let imageView : UIImageView = UIImageView(frame : CGRect(x: 0, y: 0, width: 250, height: 250 ))

 imageView.layer.borderColor = UIColor.red.cgColor
 imageView.layer.borderWidth = 1
 bottomView.addSubview(imageView)

 imageView.translatesAutoresizingMaskIntoConstraints = false
 imageView.centerXAnchor.constraint(equalTo: bottomView.centerXAnchor).isActive = true
 imageView.centerYAnchor.constraint(equalTo: bottomView.centerYAnchor).isActive = true
 imageView.widthAnchor.constraint(equalToConstant: 250).isActive = true
 imageView.heightAnchor.constraint(equalToConstant: 250).isActive = true
 imageView.downloadedFrom(link: (sizeResult?.results![0].data?.size_Chart?.mobile_image?.imageValue?.imageMain?.url)!, contentMode : .scaleAspectFill)

这个bottomView将被添加UIAlertViewController。

This image shows ** contentMode is Aspect Fit **

【问题讨论】:

  • 您是否将剪辑边界设置为真?
  • @MahendraGP 我试过了,但只有一半的图像可见..
  • 将 contentMode 设置为 aspect fit
  • 你的所有约束是否都正确......检查日志是否有破坏约束,如果有的话。
  • @ArnabHore 导致图像太小且左对齐

标签: ios swift swift3 uiimageview uiimage


【解决方案1】:

imageView?.contentMode = .scaleAspectFit

self.imageView.clipsToBounds = true

它对我有用..

【讨论】:

    【解决方案2】:

    设置uiimageview的属性和内容模式:

    self.imageView.clipsToBounds = true
    self.imageView.contentMode = .scaleAspectFit
    

    【讨论】:

      【解决方案3】:

      你的代码的最后一行应该是这个 -

      imageView.downloadedFrom(link: (sizeResult?.results![0].data?.size_Chart?.mobile_image?.imageValue?.imageMain?.url)!, contentMode : .scaleAspectFit)
      

      【讨论】:

        【解决方案4】:

        您可以使用剪辑与您的图像视图绑定,它肯定会解决您的问题。 斯威夫特:

        override func viewDidLoad() {
            super.viewDidLoad()
        
            self.bottomView.clipsToBounds = false
            self.imageView.clipsToBounds = true
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-05-08
          • 1970-01-01
          相关资源
          最近更新 更多