简体   繁体   English

图像不适合图像视图

[英]image does not fit in the image view

I am making an project in swift there is an image view and I am taking an image from gallery when I set the image in the image view the image does not match with the width and height of image view. 我在swift做一个项目有一个图像视图,当我在图像视图中设置图像时,我从图库中获取图像,图像与图像视图的宽度和高度不匹配。 overtime the image width and height is small than the image view here is the code: 超时图像宽度和高度小于图像视图这里是代码:

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!
    }

Use .aspectfill and set clipsToBounds to true 使用.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 

in your code is fitting image size in your imageview. 您的代码中的图像大小适合您的imageview。 You should use, 你应该用,

cell?.imageView?.contentMode = UIViewContentMode.scaleAspectFill

Also makes clipsToBounds=true. 还使clipsToBounds = true。

For better understanding of Imageview content modes visit http://blogs.innovationm.com/image-handling-in-ios/ 要更好地了解Imageview内容模式,请访问http://blogs.innovationm.com/image-handling-in-ios/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM