From 7edc63d06f9d7c189ddcfce7019f6b80617b9064 Mon Sep 17 00:00:00 2001 From: Selim Mustafaev Date: Tue, 22 Dec 2020 01:43:17 +0300 Subject: [PATCH] Fixes for ImageGridRow --- AutoCat/Views/ImageGrid.swift | 16 +++++++++++++--- AutoCat/Views/eureka/ImageGridRow.swift | 14 ++++++++------ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/AutoCat/Views/ImageGrid.swift b/AutoCat/Views/ImageGrid.swift index b8ca330..4e37821 100644 --- a/AutoCat/Views/ImageGrid.swift +++ b/AutoCat/Views/ImageGrid.swift @@ -13,6 +13,8 @@ class ImageCell: UICollectionViewCell { self.imgView = UIImageView() self.contentView.addSubview(self.imgView) self.imgView.translatesAutoresizingMaskIntoConstraints = false + self.imgView.contentMode = .scaleAspectFill + self.imgView.layer.masksToBounds = true NSLayoutConstraint.activate([ self.imgView.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor), self.imgView.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor), @@ -21,9 +23,18 @@ class ImageCell: UICollectionViewCell { ]) } + override func prepareForReuse() { + super.prepareForReuse() + self.imgView.kf.cancelDownloadTask() + } + func configure(with image: String) { guard let url = URL(string: image) else { return } - self.imgView.kf.setImage(with: url) + self.imgView.kf.setImage(with: url, options: [ + .processor(DownsamplingImageProcessor(size: self.imgView.frame.size)), + .scaleFactor(UIScreen.main.scale), + .cacheOriginalImage + ]) } } @@ -51,6 +62,7 @@ class ImageGrid: UICollectionView, UICollectionViewDataSource, UICollectionViewD layout.minimumLineSpacing = spacing layout.estimatedItemSize = .zero self.init(frame: .zero, collectionViewLayout: layout) + self.backgroundColor = .clear self.columnsCount = columns self.spacing = spacing } @@ -87,8 +99,6 @@ class ImageGrid: UICollectionView, UICollectionViewDataSource, UICollectionViewD func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { let cellSize = (self.bounds.size.width - self.spacing*(CGFloat(self.columnsCount) - 1))/CGFloat(self.columnsCount) - print("====================================") - print("bounds: \(self.bounds)") return CGSize(width: cellSize, height: cellSize) } } diff --git a/AutoCat/Views/eureka/ImageGridRow.swift b/AutoCat/Views/eureka/ImageGridRow.swift index 87ee45e..141eba9 100644 --- a/AutoCat/Views/eureka/ImageGridRow.swift +++ b/AutoCat/Views/eureka/ImageGridRow.swift @@ -15,19 +15,21 @@ final class ImageGridCell: Cell<[String]>, CellType { override func setup() { super.setup() - self.grid = ImageGrid(columns: 3) + self.grid = ImageGrid(columns: 3, spacing: 2) self.grid.translatesAutoresizingMaskIntoConstraints = false self.contentView.addSubview(self.grid) NSLayoutConstraint.activate([ - self.grid.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor), - self.grid.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor), - self.grid.topAnchor.constraint(equalTo: self.contentView.topAnchor), - self.grid.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor) + self.grid.leadingAnchor.constraint(equalTo: self.contentView.layoutMarginsGuide.leadingAnchor), + self.grid.trailingAnchor.constraint(equalTo: self.contentView.layoutMarginsGuide.trailingAnchor), + self.grid.topAnchor.constraint(equalTo: self.contentView.layoutMarginsGuide.topAnchor), + self.grid.bottomAnchor.constraint(equalTo: self.contentView.layoutMarginsGuide.bottomAnchor) ]) } override func update() { super.update() + self.textLabel?.text = nil + self.detailTextLabel?.text = nil self.grid.set(images: row.value ?? []) } } @@ -37,4 +39,4 @@ final class ImageGridRow: Row, RowType { super.init(tag: tag) cellProvider = CellProvider() } -} \ No newline at end of file +}