I want to add photos to one of my collection view cells similar to how snapchat does...
There are only two main requirements:
The amount of photos will vary so the overall cell height must also vary.
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
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?