简体   繁体   English

调整 UIImageView 的大小以适应屏幕

[英]Resize an UIImageView to fit in screen

There is a UIImageView passed as an property through a segue from another view controller where the user has scribbled on an image.有一个UIImageView作为属性通过来自另一个视图控制器的 segue 传递,其中用户在图像上涂鸦。 I couldn't fit / scale the image to fit in a UIView in the receiving view controller.我无法适应/缩放图像以适应接收视图控制器中的UIView No matter what I try it goes over the screen.无论我尝试什么,它都会越过屏幕。

Here are some I tried with zero success in viewDidLoad()以下是我在viewDidLoad()尝试过的零成功

incomingImgView?.frame = CGRect(x: 0, y: 0, width: view.bounds.width  , height:  view.bounds.height)
// incomingImgView?.frame = CGRect(x: 0, y: 0, width: 100  , height: 100)

incomingImgView?.contentMode = .scaleAspectFit

incomingImgView?.clipsToBounds = true

//incomingImgView?.frame = view.bounds
viewContainer.addSubview(incomingImgView!)


// incomingImgView?.image?.scaleImage(toSize: CGSize(width: 100, height: 100))
// incomingImgView?.layoutIfNeeded()

view.layoutIfNeeded()

请试试这个:

 incomingImgView?.frame = CGRect(x: 0, y: 0, width: viewContainer.bounds.width  , height:  viewContainer.bounds.height)

You are setting frame twice so the first time you are trying to set is replaced with frame incomingImgView?.frame = view.bounds so it will take the frame of view.bounds and not incomingImgView?.frame = CGRect(x: 0, y: 0, width: view.bounds.width , height: view.bounds.height) .您设置了两次框架,因此您第一次尝试设置时将替换incomingImgView?.frame = view.bounds所以它将采用view.bounds的框架而不是incomingImgView?.frame = CGRect(x: 0, y: 0, width: view.bounds.width , height: view.bounds.height)

Try the below code and check试试下面的代码并检查

   incomingImgView?.frame = CGRect(x: 0, y: 0, width: 100  , height: 100)
   incomingImgView?.contentMode = .scaleAspectFit    
   incomingImgView?.clipsToBounds = true
   viewContainer.addSubview(incomingImgView!)    

I've been working with this exact issue for a while and came up with this solution for the class that I have made which inherits from UIImageView (note that you can probably just place it as an extension of UIImageView itself instead of creating a new class if this is the only thing you're changing):我一直在处理这个确切的问题一段时间,并为我创建的类提出了这个解决方案,该类继承自 UIImageView(请注意,您可以将它作为 UIImageView 本身的扩展,而不是创建一个新类如果这是您唯一要更改的内容):

func scaleAndCenterInParent(){
    //Scales and centers to superview
    if let screenSize = superview?.frame.size{

        let frameSize = self.frame.size


        if frameSize.width > screenSize.width || frameSize.height > screenSize.height{
            //Image exceeds screen in at least one direction
            var scale: CGFloat = 1
            let frameRatio = (frameSize.width)/(frameSize.height)

            if frameRatio >= 1{
                //landscape frame
                scale = screenSize.width/frameSize.width
            }else{
                //portrait frame
                scale = screenSize.height/frameSize.height
            }

            //apply transform to self (imageview)
            self.transform = self.transform.scaledBy(x: scale, y: scale)



            //center
            self.frame.origin.x = (superview!.bounds.midX - (frameSize.width*0.5))
            self.frame.origin.y = (superview!.bounds.midY - (frameSize.height*0.5))
        }
    }
}

EDIT: Note that you still need to set the .scaleAspectFit parameter, because this only changes the size of the frame.编辑:请注意,您仍然需要设置 .scaleAspectFit 参数,因为这只会更改框架的大小。 It retains the full image quality.它保留了完整的图像质量。

你有没有试过incomingImgView.sizeToFit()这个方法使视图适合其父视图的正确大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM