Ios 如何根据UIImage大小调整UIImageView的大小并适合屏幕';s宽度
我有以下代码片段:Ios 如何根据UIImage大小调整UIImageView的大小并适合屏幕';s宽度,ios,swift,camera,uiimageview,uiimage,Ios,Swift,Camera,Uiimageview,Uiimage,我有以下代码片段: func resizeImageViewToImageSize(_ imageView:UIImageView) { let widthRatio = imageView.bounds.size.width / imageView.image!.size.width let heightRatio = imageView.bounds.size.height / imageView.image!.size.height let
func resizeImageViewToImageSize(_ imageView:UIImageView) {
let widthRatio = imageView.bounds.size.width / imageView.image!.size.width
let heightRatio = imageView.bounds.size.height / imageView.image!.size.height
let scale = min(widthRatio, heightRatio)
let imageWidth = scale * imageView.image!.size.width
let imageHeight = scale * imageView.image!.size.height
print("\(imageWidth) - \(imageHeight)")
imageView.frame = CGRect(x: 0,
y: 70,
width: imageWidth,
height: imageHeight)
imageView.center.x = view.center.x
}
我在viewDidLoad()
中调用它来调整我的originalImageView
UIImageView的大小:
resizeImageViewToImageSize(originalImageView)
它成功地调整了我的UIImageView的大小,但如果我用相机拍照(例如在iPhone7+上),我的imageWidth和imageHeight是:226.4-283.0
,这使得我的UIImageView在屏幕中央非常小,如下所示:
我想做的是让我的originalImageView
的宽度更大,并保持相应的比例,就像下面的模型一样,这是可能的吗
我认为当
imageView.image!的值!。size.width
大于imageView.bounds.size.width
或高度值
这将导致widthRatio
或heightRatio
的值小于1
当比例
小于1,并乘以图像宽度
和图像高度
时,最终的UIImageView将比原始视图小
我猜,从一个像iPhone7一样“清晰”的摄像头拍摄的图像的分辨率会很高
您必须检查哪个值更高imageView.bounds.width
或imageView.image!。size.width
,然后根据条件获取倒数
func resizeImageViewToImageSize(_ imageView:UIImageView) {
let maxWidth = view.frame.size.width
let maxHeight = view.frame.size.height
var widthRatio = imageView.bounds.size.width / imageView.image!.size.width
if widthRatio < 1 {
widthRatio = 1 / widthRatio
}
let heightRatio = imageView.bounds.size.height / imageView.image!.size.height
if heightRatio < 1 {
heightRatio = 1 / widthRatio
}
let scale = min(widthRatio, heightRatio)
let maxWidthRatio = maxWidth / imageView.bounds.size.width
let maxHeightRatio = maxHeight / imageView.bounds.size.height
let maxScale = min(maxWidthRatio, maxHeightRatio)
let properScale = min(scale, maxScale)
let imageWidth = properScale * imageView.image!.size.width
let imageHeight = properScale * imageView.image!.size.height
print("\(imageWidth) - \(imageHeight)")
imageView.frame = CGRect(x: 0,
y: 70,
width: imageWidth,
height: imageHeight)
imageView.center.x = view.center.x
}
func将imageView调整为ImageSize(imageView:UIImageView){
设maxWidth=view.frame.size.width
设maxHeight=view.frame.size.height
var widthRatio=imageView.bounds.size.width/imageView.image!.size.width
如果宽度比<1{
宽度比=1/宽度比
}
设heightRatio=imageView.bounds.size.height/imageView.image!.size.height
如果高度比<1{
高度比=1/宽度比
}
让刻度=最小值(宽高比)
设maxWidthRatio=maxWidth/imageView.bounds.size.width
设maxHeightRatio=maxHeight/imageView.bounds.size.height
设maxScale=min(maxWidthRatio,maxHeightRatio)
让properScale=min(比例,最大比例)
让imageWidth=properScale*imageView.image!.size.width
让imageHeight=properScale*imageView.image!.size.height
打印(“\(图像宽度)-\(图像高度)”)
imageView.frame=CGRect(x:0,
y:70,
宽度:图像宽度,
高度:图像高度)
imageView.center.x=view.center.x
}
我认为当imageView.image!的值!。size.width
大于imageView.bounds.size.width
或高度值
这将导致widthRatio
或heightRatio
的值小于1
当比例
小于1,并乘以图像宽度
和图像高度
时,最终的UIImageView将比原始视图小
我猜,从一个像iPhone7一样“清晰”的摄像头拍摄的图像的分辨率会很高
您必须检查哪个值更高imageView.bounds.width
或imageView.image!。size.width
,然后根据条件获取倒数
func resizeImageViewToImageSize(_ imageView:UIImageView) {
let maxWidth = view.frame.size.width
let maxHeight = view.frame.size.height
var widthRatio = imageView.bounds.size.width / imageView.image!.size.width
if widthRatio < 1 {
widthRatio = 1 / widthRatio
}
let heightRatio = imageView.bounds.size.height / imageView.image!.size.height
if heightRatio < 1 {
heightRatio = 1 / widthRatio
}
let scale = min(widthRatio, heightRatio)
let maxWidthRatio = maxWidth / imageView.bounds.size.width
let maxHeightRatio = maxHeight / imageView.bounds.size.height
let maxScale = min(maxWidthRatio, maxHeightRatio)
let properScale = min(scale, maxScale)
let imageWidth = properScale * imageView.image!.size.width
let imageHeight = properScale * imageView.image!.size.height
print("\(imageWidth) - \(imageHeight)")
imageView.frame = CGRect(x: 0,
y: 70,
width: imageWidth,
height: imageHeight)
imageView.center.x = view.center.x
}
func将imageView调整为ImageSize(imageView:UIImageView){
设maxWidth=view.frame.size.width
设maxHeight=view.frame.size.height
var widthRatio=imageView.bounds.size.width/imageView.image!.size.width
如果宽度比<1{
宽度比=1/宽度比
}
设heightRatio=imageView.bounds.size.height/imageView.image!.size.height
如果高度比<1{
高度比=1/宽度比
}
让刻度=最小值(宽高比)
设maxWidthRatio=maxWidth/imageView.bounds.size.width
设maxHeightRatio=maxHeight/imageView.bounds.size.height
设maxScale=min(maxWidthRatio,maxHeightRatio)
让properScale=min(比例,最大比例)
让imageWidth=properScale*imageView.image!.size.width
让imageHeight=properScale*imageView.image!.size.height
打印(“\(图像宽度)-\(图像高度)”)
imageView.frame=CGRect(x:0,
y:70,
宽度:图像宽度,
高度:图像高度)
imageView.center.x=view.center.x
}
谢谢你的回答,我该怎么办?我正在iPhone 7+大屏幕上测试我的代码,我必须将imageWidth*2乘以imageHeight?我以前也这么做过,但是更大的图像呢?我想你可以在屏幕上获得最大的宽度和高度。因为那真的是武断的。除非您计划能够平移和缩放图像?(然后您仍然可以设置最大值或比例值)。但我想建议的下一步是:1。检查哪个更大:imageView.bounds.size.width
或imageView.image!。尺寸。宽度
2。使用较大的值作为分子,较小的值作为分母(以确保按比例放大)3。对高度4也要这样做<代码>比例=最小值(宽高比)5。获取最大比例(屏幕宽度/imageView.bounds.size.width)(?)6usableScale=max(scale,maxScale)
7<代码>图像宽度=usableScale*imageView.image!。尺寸.宽度8。Height抱歉,我是第一次回答和评论没有问题,但是请用你的评论代码更新你的答案:)谢谢你的回答,那么我该怎么办?我正在iPhone 7+大屏幕上测试我的代码,我必须将imageWidth*2乘以imageHeight?我以前也这么做过,但是更大的图像呢?我想你可以在屏幕上获得最大的宽度和高度。因为那真的是武断的。除非您计划能够平移和缩放图像?(然后您仍然可以设置最大值或比例值)。但我想建议的下一步是:1。检查哪个是l