UIImageView with Aspect Fill inside custom UITableViewCell using AutoLayout

后端 未结 2 1048
情话喂你
情话喂你 2021-02-04 17:10

I\'ve been struggling with fitting an UIImageView which shows images of variable widths and heights with Aspect Fill. The cell height is not adapting t

2条回答
  •  没有蜡笔的小新
    2021-02-04 17:18

    Suppose you have an UIImage with dimensions 768 x 592 of width and height respectively, the height always remains equals, where the device it's rotated for example in the dimensions above (iPad), the width of the image change to 1024 and the height remains equal.

    What you can do to maintain the aspect of the image is scale it in the dimensions you want, for example if you know that the images coming always have the same dimensions, we say for example 1280x740 , you can set the UIImage to .ScaleToFill and calculate in the following way:

    (widthOfTheImage / heightOfTheImage) * heightWhereYouWanToSet = widthYouHaveToSet
    

    For example :

    (1280 / 740) * 592 = 1024
    

    And it's the width I have to set in my UIImage to maintain the proportions of the image when it's change it's width.

    I hope you understand where I try to say to you.

提交回复
热议问题