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. 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
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. You should use,
cell?.imageView?.contentMode = UIViewContentMode.scaleAspectFill
Also makes clipsToBounds=true.
For better understanding of Imageview content modes visit http://blogs.innovationm.com/image-handling-in-ios/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.