Commit 687ec259 by Dmitriy Stepanets

Chnaged shorts overlay image rendering to fullscreen

parent 35fbac87
......@@ -70,10 +70,41 @@ class ShortsItemCell: UITableViewCell {
currentShorts = shortsItem
if let backgroundImage = shortsItem.getOverlayImage(for: tableWidth) {
Nuke.loadImage(with: backgroundImage.url, into: backgroundImageView)
Nuke.loadImage(with: backgroundImage.url, into: backgroundImageView) {[weak self] result in
guard let self = self else { return }
switch result {
case .success(let imageResponse):
onMain {
self.backgroundImageView.image = imageResponse.image
self.ctaButton.isHidden = false
self.likeButton.isHidden = false
//Update ImageView size
let aspect = imageResponse.image.size.width / imageResponse.image.size.height
self.backgroundImageView.snp.remakeConstraints { remake in
remake.left.top.right.equalToSuperview()
remake.bottom.equalToSuperview().priority(.medium)
remake.width.equalTo(self.backgroundImageView.snp.height).multipliedBy(aspect)
}
if let cachedColor = self.delegate?.averageColor(forImage: imageResponse.image,
identifier: backgroundImage.url.absoluteString) {
self.contentView.backgroundColor = cachedColor
}
else {
self.contentView.backgroundColor = ThemeManager.currentTheme.baseBackgroundColor
}
}
default:
self.ctaButton.isHidden = true
self.likeButton.isHidden = true
}
}
}
else {
backgroundImageView.backgroundColor = UIColor.lightGray
self.ctaButton.isHidden = true
self.likeButton.isHidden = true
contentView.backgroundColor = ThemeManager.currentTheme.baseBackgroundColor
}
UIView.performWithoutAnimation {
......@@ -133,10 +164,13 @@ class ShortsItemCell: UITableViewCell {
//MARK:- Prepare
private extension ShortsItemCell {
func prepareForImageOnly() {
contentView.backgroundColor = ThemeManager.currentTheme.baseBackgroundColor
backgroundImageView.contentMode = .scaleAspectFit
contentView.addSubview(backgroundImageView)
backgroundImageView.snp.makeConstraints { make in
make.edges.equalToSuperview()
make.left.top.right.equalToSuperview()
make.bottom.equalToSuperview().priority(.medium)
make.width.equalTo(backgroundImageView.snp.height).multipliedBy(1)
}
}
......@@ -228,6 +262,7 @@ private extension ShortsItemCell {
}
func prepareActionButtons() {
likeButton.isHidden = true
likeButton.addTarget(self, action: #selector(handleLikeButton), for: .touchUpInside)
likeButton.setImage(UIImage(named: "like_outline"), for: .normal)
likeButton.backgroundColor = ThemeManager.currentTheme.baseBackgroundColor
......@@ -239,6 +274,7 @@ private extension ShortsItemCell {
likeButton.layer.shadowOpacity = 0.3
contentView.addSubview(likeButton)
ctaButton.isHidden = true
ctaButton.addTarget(self, action: #selector(handleCtaButton), for: .touchUpInside)
ctaButton.backgroundColor = .clear
ctaButton.layer.borderColor = UIColor(hex: 0xDDDDDD).cgColor
......
......@@ -266,10 +266,10 @@ extension ShortsViewController: UITableViewDataSource {
//MARK:- UITableView Delegate
extension ShortsViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if let image = viewModel.shorts[indexPath.row].getOverlayImage(for: tableView.frame.width) {
let aspectRatio = CGFloat(image.width) / CGFloat(image.height)
return tableView.bounds.width / aspectRatio
}
// if let image = viewModel.shorts[indexPath.row].getOverlayImage(for: tableView.frame.width) {
// let aspectRatio = CGFloat(image.width) / CGFloat(image.height)
// return tableView.bounds.width / aspectRatio
// }
return tableView.bounds.height
}
......@@ -355,13 +355,27 @@ extension ShortsViewController: ViewModelDelegate {
//MARK:- ShortsItemCell Delegate
extension ShortsViewController: ShortsItemCellDelegate {
private func bottomPart(of sourceImage: UIImage) -> UIImage? {
let dy: CGFloat = 10 //10pt from bottom
let area = CGRect(x: 0,
y: sourceImage.size.height - dy,
width: sourceImage.size.width,
height: dy)
guard let crop = sourceImage.cgImage?.cropping(to: area) else {
return nil
}
return UIImage(cgImage: crop, scale: UIScreen.main.scale, orientation: .up)
}
func averageColor(forImage image: UIImage, identifier: String) -> UIColor? {
if let cachedColor = self.averageColorCache[identifier] {
return cachedColor
}
else {
if let color = image.averageColor {
self.averageColorCache[identifier] = color.isDarkColor ? color : color.darker(by: 30)
if let bottomPartImage = self.bottomPart(of: image),
let color = bottomPartImage.averageColor {
self.averageColorCache[identifier] = color
return color
}
else {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment