我正在 swift 中制作一个项目,有一个图像视图,当我在图像视图中设置图像时,我正在从图库中获取图像,图像与图像视图的宽度和高度不匹配。 加班 图像宽度和高度小于图像视图这是代码:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "ImageTVCell"
index=indexPath.row
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? ImageTVCell
// cell?.imageView?.clipsToBounds=true
cell?.imageView?.contentMode = UIViewContentMode.scaleAspectFit
cell?.imageView?.image=imageArray[indexPath.row]
return cell!
}
使用 .aspectfill
并将clipsToBounds
设置为 true
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "ImageTVCell"
index=indexPath.row
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? ImageTVCell
cell?.imageView?.clipsToBounds=true
cell?.imageView?.contentMode = UIViewContentMode.scaleAspectFill
cell?.imageView?.image=imageArray[indexPath.row]
return cell!
}
cell?.imageView?.contentMode = UIViewContentMode.scaleAspectFit
在您的代码中适合图像视图中的图像大小。你应该使用,
cell?.imageView?.contentMode = UIViewContentMode.scaleAspectFill
也使 clipsToBounds=true。
为了更好地了解图像视图内容模式,请访问http://blogs.innovationm.com/image-handling-in-ios/