gpt4 book ai didi

ios - Objective-C - 设置 ImageView 宽度并保持纵横比

转载 作者:行者123 更新时间:2023-11-28 21:39:59 24 4
gpt4 key购买 nike

我正在使用以下动画在 View 上显示缩略图的全屏图像:

[UIView animateWithDuration:0.5 delay:0 options:0 animations:^{

[testImageView setFrame:CGRectMake(0, 0, testImage.size.width, testImage.size.height)];

}completion:^(BOOL finished){

}];

但是,如果是大图像,这将不包含屏幕边界内的图像。

有没有一种方法可以将图像的宽度设置为屏幕的宽度,然后调整图像的高度以保持适当的纵横比?

最佳答案

与上述方法类似,但有更多修改:

CGFloat verticalScale = image.height / bounds.height;
CGFloat horizontalScale = image.width / bounds.width;
CGFloat scale = max(verticalScale,horizontalScale);
CGFloat imageViewHeight = image.height / scale;
CGFloat imageViewWidth = image.width / scale;
CGRect imageViewFrame = CGRectMake(x: DESIRE_X, y: DESIRE_Y, width:imageViewWidth ,height:imageViewHeight)
imageView.frame = imageViewFrame;
imageView.contentMode = UIViewContentModeScaleAspectFill;

关于ios - Objective-C - 设置 ImageView 宽度并保持纵横比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32545368/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com