Swift UIImageView Stretched Aspect [英] Swift UIImageView Stretched Aspect

查看:78
本文介绍了Swift UIImageView Stretched Aspect的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UIImageView错误地渲染图像的大小。使用Scale Aspect Fit,如果UIImageView是正方形,则图像是正确的宽高比,图像未填充的区域具有透明度。

UIImageView renders the size of an image incorrectly. Using Scale Aspect Fit, if the UIImageView is a square the image is the correct aspect ratio with transparency in the areas the image does not fill.

//Image is Square & Correct Size
var imageView = UIImageView(frame: CGRectMake(0, 50, 320, 320))
imageView.clipsToBounds = true
imageView.contentMode = UIViewContentMode.ScaleAspectFit

//Image is Rectangle & Incorrect Size
var imageView = UIImageView(frame: CGRectMake(0, 50, 320, 450))
imageView.clipsToBounds = true
imageView.contentMode = UIViewContentMode.ScaleAspectFit

UIImageView需要触摸边缘并在屏幕的顶部和底部有透明空间,内部图像需要保持其原始比例而不是拉伸更高。我附上了两张UIImageView中的图像渲染图像。

The UIImageView needs to touch the edges and have transparent space at the top and bottom of the screen and the image inside needs to keep its original ratio rather than stretching taller. I have attached two images of how the image inside the UIImageView is rendering.


推荐答案

我在UIImageView中添加了一个自动调整掩码,它现在显示正确的比率。

I added an autoresizing mask to the UIImageView and it now displays the correct ratios.

imageView.autoresizingMask = UIViewAutoresizing.FlexibleBottomMargin | UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleRightMargin | UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleTopMargin | UIViewAutoresizing.FlexibleWidth
imageView.contentMode = UIViewContentMode.ScaleAspectFit

这个答案帮助我:使用AVCaptureSession sessionPreset = AVCaptureSessionPresetPhoto 捕捉被捕获的照片通过手机摄像头拍摄的图像。

This answer helped me: Captured photo is stretched with AVCaptureSession sessionPreset = AVCaptureSessionPresetPhoto as I was using an image that was taken through the phones camera.

这篇关于Swift UIImageView Stretched Aspect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆