这里黑色边框显示了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。
答案 0 :(得分:4)
您可以使用剪辑与图像视图绑定,这肯定会解决您的问题。 斯威夫特:
override func viewDidLoad() {
super.viewDidLoad()
self.bottomView.clipsToBounds = false
self.imageView.clipsToBounds = true
}
答案 1 :(得分:1)
设置uiimageview
和内容模式的属性:
self.imageView.clipsToBounds = true
self.imageView.contentMode = .scaleAspectFit
答案 2 :(得分:0)
您的代码的最后一行应为-
imageView.downloadedFrom(link: (sizeResult?.results![0].data?.size_Chart?.mobile_image?.imageValue?.imageMain?.url)!, contentMode : .scaleAspectFit)
答案 3 :(得分:0)
imageView?.contentMode = .scaleAspectFit
self.imageView.clipsToBounds = true
对我来说是工作。