菜鸟教程小白 发表于 2022-12-12 09:22:05

ios - 如何在 AspectFit 比率后调整 UIImageView 的大小


                                            <p><p>如何在将 UIImageView 的 contentmode 设置为 <code>UIViewContentModeScaleAspectFit</code> 后调整 UIImageView 的大小,以便我可以从顶部和底部删除空格。
请看附图
<br/> <img src="/image/cb4ej.png" alt="image"/> </p>
<p>提前致谢:)</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>希望这对其他人也有帮助,如果它在某些情况下会中断,请发表评论:) </p>

<pre><code>- (CGRect)frameForImageattribute:(CGSize)image inImageViewAspectFit:(UIImageView *)imageView {
      float imageRatio = image.width / image.height;
      float viewRatio = imageView.frame.size.width / imageView.frame.size.height;
      if (imageRatio &lt; viewRatio) {
            float scale = imageView.frame.size.height / image.height;
            float width = scale * image.width;
            return CGRectMake(kLeftPading, kTopPading, width, imageView.frame.size.height);
      }
      else {
            float scale = imageView.frame.size.width / image.width;
            float height = scale * image.height;
            return CGRectMake(kLeftPading, kTopPading, imageView.frame.size.width, height);
      }
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何在 AspectFit 比率后调整 UIImageView 的大小,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/23042546/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/23042546/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何在 AspectFit 比率后调整 UIImageView 的大小