Skip to content

Instantly share code, notes, and snippets.

@christopherkarani
Created November 19, 2018 18:57
Show Gist options
  • Save christopherkarani/59b9d3f9a0e24c3f92a11f46f3f0686f to your computer and use it in GitHub Desktop.
Save christopherkarani/59b9d3f9a0e24c3f92a11f46f3f0686f to your computer and use it in GitHub Desktop.
/// A little pieve of genius by me that culculates cell size base on the image given while maintaining aspect ratio
open class MediaMessageSizeCalculator {
open func messageContainerSize(for image: UIImage) -> CGSize {
let maxWidth = UIScreen.main.bounds.width - 16
let sizeForMediaItem = { (maxWidth: CGFloat, item: UIImage) -> CGSize in
if maxWidth < item.size.width {
// Maintain the ratio if width is too great
let height = maxWidth * item.size.height / item.size.width
return CGSize(width: maxWidth, height: height)
}
return item.size
}
return sizeForMediaItem(maxWidth, image)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment