7.4 C
London
Wednesday, April 24, 2024

ios – use UICollectionViewCompositionalLayout to render a distant picture correctly


I wish to show a NSCollectionLayoutItem occupy the system display screen horizontally , and nonetheless get the right side ratio in Vertical .

So I exploit the NSCollectionLayoutDimension.estimated(50) factors for top . however after the picture is downloaded , the peak continues to be 50 ,which isn’t sufficient for show the picture .

make the structure robotically match the picture’s content material.

the picture hyperlink is right here : picture src

and the consequence might be like this

enter image description here

the UICollectionView configuration code :

import UIKit

class ViewController: UIViewController {
    enum Part {
        case major
    }
    
    @IBOutlet weak var collectionView: UICollectionView!
    var dataSource: UICollectionViewDiffableDataSource<Part, Int>! = nil
    override func viewDidLoad() {
        tremendous.viewDidLoad()
        // Do any extra setup after loading the view.
        self.configDataSource()
        self.configLayout()
    }

    
    func configDataSource() {
        let imageCellReg = UICollectionView.CellRegistration<ImageCell,Int>(cellNib: UINib(nibName: "ImageCell", bundle: nil)) { cell, indexPath, itemIdentifier in
            
        }
        self.dataSource = UICollectionViewDiffableDataSource<Part, Int>(collectionView: self.collectionView, cellProvider: { collectionView, indexPath, itemIdentifier in
            return self.collectionView.dequeueConfiguredReusableCell(utilizing: imageCellReg, for: indexPath, merchandise: itemIdentifier)
        })
        
        var snapshot = NSDiffableDataSourceSnapshot<Part, Int>()
        snapshot.appendSections([.main])
        snapshot.appendItems(Array(0..<2))
        dataSource.apply(snapshot, animatingDifferences: false)

    }
    
    func configLayout(){
        let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
                                             heightDimension: .fractionalHeight(1.0))
        let merchandise = NSCollectionLayoutItem(layoutSize: itemSize)

        let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
                                               heightDimension:.estimated(50))
        let group = NSCollectionLayoutGroup.horizontal(layoutSize:groupSize, subitems: [item])
        let spacing = CGFloat(10)
        group.interItemSpacing = .fastened(spacing)

        let part = NSCollectionLayoutSection(group: group)
        part.interGroupSpacing = spacing
        part.contentInsets = NSDirectionalEdgeInsets(prime: 0, main: 10, backside: 0, trailing: 10)

        let structure = UICollectionViewCompositionalLayout(part: part)
        self.collectionView.collectionViewLayout = structure
    }

}

And the cell code:

import UIKit
import SDWebImage

class ImageCell: UICollectionViewCell {
    @IBOutlet weak var imageContentView: UIImageView!
    
    override func awakeFromNib() {
        tremendous.awakeFromNib()
        // Initialization code
        imageContentView.sd_setImage(with: URL(string: "https://www.google.com/photographs/branding/googlelogo/2x/googlelogo_color_272x92dp.png"))
    }

}

Latest news
Related news

LEAVE A REPLY

Please enter your comment!
Please enter your name here