|
| 1 | +// |
| 2 | +// LUXImageTableViewCell.swift |
| 3 | +// LUX |
| 4 | +// |
| 5 | +// Created by Calvin Collins on 12/3/21. |
| 6 | +// |
| 7 | + |
| 8 | +import UIKit |
| 9 | +import SDWebImage |
| 10 | +import Prelude |
| 11 | +import LithoOperators |
| 12 | + |
| 13 | +open class LUXImageTableViewCell: UITableViewCell { |
| 14 | + @IBOutlet weak public var contentImageView: UIImageView? |
| 15 | + @IBOutlet weak public var imageTopMargin: NSLayoutConstraint? |
| 16 | + @IBOutlet weak public var imageLeadingMargin: NSLayoutConstraint? |
| 17 | + @IBOutlet weak public var imageTrailingMargin: NSLayoutConstraint? |
| 18 | + @IBOutlet weak public var imageBottomMargin: NSLayoutConstraint? |
| 19 | + @IBOutlet weak public var imageHeightConstraint: NSLayoutConstraint? |
| 20 | + @IBOutlet weak public var imageWidthConstraint: NSLayoutConstraint? |
| 21 | + |
| 22 | + /** |
| 23 | + * The LUX provided XIB for this class does not include this constraint, as it could conflict with width/height constraints. |
| 24 | + * It is provided here for storing an aspect ratio constraint, or for subclasses that want to use it as an outlet. |
| 25 | + * If you'd like to use aspect ratio instead of width/height, we recommend looking at the code in bindUrlStringToCell, or |
| 26 | + * just using bindUrlString. |
| 27 | + */ |
| 28 | + @IBOutlet weak public var aspectRatioConstraint: NSLayoutConstraint? |
| 29 | + |
| 30 | + open override func awakeFromNib() { |
| 31 | + super.awakeFromNib() |
| 32 | + // Initialization code |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +public func bindUrlString<CellType>(to imageView: UIImageView?, |
| 37 | + from urlString: String, |
| 38 | + storingConstraintIn kp: WritableKeyPath<CellType, NSLayoutConstraint?>, |
| 39 | + on cell: CellType) |
| 40 | +where CellType: UITableViewCell { |
| 41 | + if let url = URL(string: urlString) { |
| 42 | + imageView?.sd_setImage(with: url, completed: { image, _, _, _ in |
| 43 | + let tableView = cell.superview as? UITableView |
| 44 | + tableView?.beginUpdates() |
| 45 | + |
| 46 | + cell |> set(kp, applyRatioImageConstraint(to: imageView, for: image, removing: cell[keyPath: kp])) |
| 47 | + |
| 48 | + tableView?.endUpdates() |
| 49 | + }) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +public func bindUrlStringToCell(_ cell: LUXImageTableViewCell, urlString: String) { |
| 54 | + if let url = URL(string: urlString) { |
| 55 | + cell.contentImageView?.sd_setImage(with: url, completed: { image, _, _, _ in |
| 56 | + if let imageView = cell.contentImageView { |
| 57 | + cell.imageHeightConstraint ?> imageView.removeConstraint(_:) |
| 58 | + cell.imageWidthConstraint ?> imageView.removeConstraint(_:) |
| 59 | + } |
| 60 | + |
| 61 | + let tableView = cell.superview as? UITableView |
| 62 | + tableView?.beginUpdates() |
| 63 | + |
| 64 | + cell.aspectRatioConstraint = applyRatioImageConstraint(to: cell.contentImageView, for: image, removing: cell.aspectRatioConstraint) |
| 65 | + |
| 66 | + tableView?.endUpdates() |
| 67 | + }) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +public func applyRatioImageConstraint(to imageView: UIImageView?, for image: UIImage?, removing oldConstraint: NSLayoutConstraint?) -> NSLayoutConstraint? { |
| 72 | + if let imageView = imageView, let image = image { |
| 73 | + let imageSize = image.size |
| 74 | + let ratio = imageSize.height / imageSize.width |
| 75 | + |
| 76 | + if let oldConstraint = oldConstraint { |
| 77 | + imageView.removeConstraint(oldConstraint) |
| 78 | + } |
| 79 | + |
| 80 | + imageView.translatesAutoresizingMaskIntoConstraints = false |
| 81 | + |
| 82 | + let newConstraint = NSLayoutConstraint(item: imageView, |
| 83 | + attribute: .height, |
| 84 | + relatedBy: .equal, |
| 85 | + toItem: imageView, |
| 86 | + attribute: .width, |
| 87 | + multiplier: ratio, |
| 88 | + constant: 0) |
| 89 | + imageView.addConstraint(newConstraint) |
| 90 | + NSLayoutConstraint.activate([newConstraint]) |
| 91 | + |
| 92 | + return newConstraint |
| 93 | + } |
| 94 | + |
| 95 | + return nil |
| 96 | +} |
0 commit comments