OGeek|极客世界-中国程序员成长平台

标题: ios - 如何在 AspectFit 比率后调整 UIImageView 的大小 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 09:22
标题: ios - 如何在 AspectFit 比率后调整 UIImageView 的大小

如何在将 UIImageView 的 contentmode 设置为 UIViewContentModeScaleAspectFit 后调整 UIImageView 的大小,以便我可以从顶部和底部删除空格。 请看附图
image

提前致谢



Best Answer-推荐答案


希望这对其他人也有帮助,如果它在某些情况下会中断,请发表评论

- (CGRect)frameForImageattributeCGSize)image inImageViewAspectFitUIImageView *)imageView {
        float imageRatio = image.width / image.height;
        float viewRatio = imageView.frame.size.width / imageView.frame.size.height;
        if (imageRatio < 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);
        }
}

关于ios - 如何在 AspectFit 比率后调整 UIImageView 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23042546/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4