Fix another crash in dynamic height calculation
This commit is contained in:
@@ -64,9 +64,12 @@ struct ImageCarousel: View {
|
|||||||
view.framePreloadCount = 3
|
view.framePreloadCount = 3
|
||||||
}
|
}
|
||||||
.imageModifier({ img in
|
.imageModifier({ img in
|
||||||
let fill = calculate_image_fill(geo: geo, img: img, maxHeight: maxHeight, minHeight: minHeight)
|
let img_size = img.size
|
||||||
|
let is_animated = img.kf.imageFrameCount != nil
|
||||||
|
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
|
let fill = calculate_image_fill(geo: geo, img_size: img_size, is_animated: is_animated, maxHeight: maxHeight, minHeight: minHeight)
|
||||||
|
|
||||||
if let filling = fill.filling {
|
if let filling = fill.filling {
|
||||||
self.filling = filling
|
self.filling = filling
|
||||||
}
|
}
|
||||||
@@ -112,32 +115,32 @@ struct ImageFill {
|
|||||||
let height: CGFloat
|
let height: CGFloat
|
||||||
}
|
}
|
||||||
|
|
||||||
func calculate_image_fill(geo: GeometryProxy, img: UIImage, maxHeight: CGFloat, minHeight: CGFloat) -> ImageFill {
|
func calculate_image_fill(geo: GeometryProxy, img_size: CGSize, is_animated: Bool, maxHeight: CGFloat, minHeight: CGFloat) -> ImageFill {
|
||||||
let shape = determine_image_shape(img.size)
|
let shape = determine_image_shape(img_size)
|
||||||
|
|
||||||
let xfactor = geo.size.width / img.size.width
|
let xfactor = geo.size.width / img_size.width
|
||||||
let yfactor = maxHeight / img.size.height
|
let yfactor = maxHeight / img_size.height
|
||||||
// calculate scaled image height
|
// calculate scaled image height
|
||||||
// set scale factor and constrain images to minimum 150
|
// set scale factor and constrain images to minimum 150
|
||||||
// and animations to scaled factor for dynamic size adjustment
|
// and animations to scaled factor for dynamic size adjustment
|
||||||
switch shape {
|
switch shape {
|
||||||
case .portrait:
|
case .portrait:
|
||||||
let filling = yfactor <= 1.0
|
let filling = yfactor <= 1.0
|
||||||
let scaled = img.size.height * xfactor
|
let scaled = img_size.height * xfactor
|
||||||
let height = filling ? maxHeight : max(scaled, minHeight)
|
let height = filling ? maxHeight : max(scaled, minHeight)
|
||||||
return ImageFill(filling: filling, height: height)
|
return ImageFill(filling: filling, height: height)
|
||||||
case .square:
|
case .square:
|
||||||
let filling = yfactor <= 1.0 && xfactor <= 1.0
|
let filling = yfactor <= 1.0 && xfactor <= 1.0
|
||||||
let scaled = img.size.height * xfactor
|
let scaled = img_size.height * xfactor
|
||||||
let height = filling ? maxHeight : max(scaled, minHeight)
|
let height = filling ? maxHeight : max(scaled, minHeight)
|
||||||
return ImageFill(filling: filling, height: height)
|
return ImageFill(filling: filling, height: height)
|
||||||
case .landscape:
|
case .landscape:
|
||||||
let scaled = img.size.height * xfactor
|
let scaled = img_size.height * xfactor
|
||||||
let filling = scaled > maxHeight || xfactor < 1.0
|
let filling = scaled > maxHeight || xfactor < 1.0
|
||||||
let height = img.kf.imageFrameCount != nil ? scaled : filling ? min(maxHeight, scaled) : max(scaled, minHeight)
|
let height = is_animated ? scaled : filling ? min(maxHeight, scaled) : max(scaled, minHeight)
|
||||||
return ImageFill(filling: filling, height: height)
|
return ImageFill(filling: filling, height: height)
|
||||||
case .unknown:
|
case .unknown:
|
||||||
let height = max(img.size.height, minHeight)
|
let height = max(img_size.height, minHeight)
|
||||||
return ImageFill(filling: nil, height: height)
|
return ImageFill(filling: nil, height: height)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user