Created
April 21, 2017 05:58
-
-
Save bwoods/120828824ee3ad6b7644216fc42a766e to your computer and use it in GitHub Desktop.
UITableViewCell with bottom-centered fixed-width image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MARK: - UITableViewDelegate methods | |
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { | |
let margin: CGFloat = 8 | |
var size = CGSize(width: cell.bounds.height - margin, height: cell.bounds.height - margin) | |
if cell.imageView!.image!.size.width > cell.imageView!.image!.size.height { | |
let factor = cell.imageView!.image!.size.width / cell.imageView!.image!.size.height; | |
size.height = size.height / factor; | |
} | |
else { | |
let factor = cell.imageView!.image!.size.height / cell.imageView!.image!.size.width; | |
size.width = size.width / factor; | |
} | |
UIGraphicsBeginImageContextWithOptions(CGSize(width: cell.bounds.height, height: cell.bounds.height), false, 0) | |
let frame = CGRect(x: (cell.bounds.height - size.width) / 2, y: (cell.bounds.height - size.height) - margin/4, width: size.width, height: size.height) | |
cell.imageView?.image!.draw(in: frame) | |
cell.imageView?.image = UIGraphicsGetImageFromCurrentImageContext()! | |
UIGraphicsEndImageContext() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment