ios UIImage 超出 UIImageView 边框

标签 ios swift swift3 uiimageview uiimage

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

enter image description here

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 **

最佳答案

你可以使用clip绑定(bind)你的image view,绝对可以解决你的问题。 swift :

override func viewDidLoad() {
    super.viewDidLoad()

    self.bottomView.clipsToBounds = false
    self.imageView.clipsToBounds = true
}

关于ios UIImage 超出 UIImageView 边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50982423/

相关文章:

ios - 使用 Aspect Fit 时如何去除图像中的空白区域?

macos - 在 Swift 中使用 NSInputStream 从 STDIN 获取输入

swift - 如何在 Swift 中检查/谓词函数类型?

ios - 二进制字符串到人类可读字符串

ios - 单击swift 4中的单选按钮时如何获取 TableView 索引?

ios - (SWIFT 3) 创建父子托管对象上下文

ios - 将更新的图像保存到 PhotoKit 时缺少图像元数据

ios - BlueCap Kit BLE 在 iOS 中的实现

ios - Xcode Reverted to old version,如何恢复?

iphone - UITableViewController不会显示创建时的单元格