0

I want to add photos to one of my collection view cells similar to how snapchat does...

enter image description here

There are only two main requirements:

  1. The amount of photos will vary so the overall cell height must also vary.

  2. Each photo should maintain its aspect ratio.

My initial thought was to the array of images to be displayed and right before the cell is dequeued, calculate each cells size maintaining it's aspect ratio and then using that data to present the images inside a collection view inside of the cell. However my results are lacking.

The images seem to be sizing properly, however the image layout handled by UICollectionViewFlowLayout doesn't place the images properly

enter image description here enter image description here

Here is the method that calculate the sizing.

func calculateImageSize(images: [UIImage], bounds: CGRect) -> [CGSize] {

    var imageSizes: [CGSize] = []

    var imageWidth: Int!
    var imageHeight: Int!
    var aspectRatio: CGSize!
    var convertedRect: CGRect!

    for i in 0..<images.count {

        imageWidth = images[i].cgImage?.width
        imageHeight = images[i].cgImage?.height

        aspectRatio = CGSize(width: imageWidth, height: imageHeight)
        convertedRect = AVMakeRect(aspectRatio: aspectRatio, insideRect: bounds)

        imageSizes.append(CGSize(width: convertedRect.width, height: convertedRect.height))
    }
return imageSizes
}

Both the minimum line and inter-item spacing are set to zero.

Any suggestions?

4

1 回答 1

0

我不认为这与图像大小有关。这是您需要自定义的布局。我进行了快速搜索,发现这个库可能会有所帮助:https ://github.com/rubygarage/collection-view-layouts

于 2018-10-31T06:55:10.957 回答