Swift3 如何修复从Swift代码设置时的图像缩放

Swift3 如何修复从Swift代码设置时的图像缩放,swift3,uiimageview,ios10,Swift3,Uiimageview,Ios10,我试图从我的Swift代码中设置图像,并在表格视图单元格中显示它们,但当我从Swift代码中设置图像时,它们被缩放或位于完全不同的位置 有人有类似的问题吗 我的代码: var datasources = ["mapmyrun","fitbitlogo"] func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { guard let cell

我试图从我的Swift代码中设置图像,并在表格视图单元格中显示它们,但当我从Swift代码中设置图像时,它们被缩放或位于完全不同的位置

有人有类似的问题吗

我的代码:

var datasources = ["mapmyrun","fitbitlogo"]



func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    guard let cell = tableview.dequeueReusableCell(withIdentifier: "datasourcecell", for: indexPath) as? DataSourceCell else{
        fatalError("cell is not an instance of DataSourceCell")
    }
    cell.imageView?.contentMode =  UIViewContentMode.scaleAspectFit
    let image = UIImage(named: datasources[indexPath.row])

    cell.imageView?.image = image

    return cell


}
代码的结果:

手动设置时的结果。它非常适合:


是否尝试填充覆盖整个单元格的图像?您是否在
datasourcecell
类中为
imageView
连接了任何
IB插座
。我想您似乎没有向imageView添加任何自动布局约束。添加前导、尾随、顶部和底部约束,并将图像内容模式设置为aspect fit。它应该会起作用。