2017-01-11 54 views
0

有一个UIImageView作为属性通过从其他用户在图像上涂写的视图控制器的segue传递。我无法在接收视图控制器中适合/缩放图像以适应UIView。无论我尝试什么,它都会在屏幕上显示。调整UIImageView的大小以适应屏幕

这里有一些我与零次的成功尝试在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() 
+3

为什么你在两个UIViews之间传递UIImageView?为什么不只是传递一个UIImage,让每个UIView按照你想要的方式进行尺寸调整? – dfd

+1

@CodeTree请不要做那样的事情。传递数据 - 而不是接口。 –

回答

1

请试试这个:

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

no:| 这是怎么代表,但不是工作 incomingImgView .frame =的CGRect?(X:0,Y:0,宽度:viewContainer.bounds.width,高度:viewContainer.bounds.height)? incomingImgView .contentMode = .scaleAspectFit viewContainer.addSubview(!incomingImgView) view.layoutIfNeeded() –

+0

请加入这一行,并尝试 - viewContainer.layoutIfNeeded() – CrazyVK56

+0

已经尝试过:| –

0

要设置帧两次,你试图设置第一次被替换为框架因此它将采用view.bounds的框架而不是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 
    viewContainer.addSubview(incomingImgView!)  
+0

对不起!这是粘贴代码时,错误,编辑它,不工作 –

+0

@CodeTree你可以告诉我你是如何通过从segue imageView? –

+0

如果让validVC:FinalViewController = {VC如果 让capturedImage = {imgView validVC.incomingImgView = capturedImage self.navigationController .pushViewController(validVC,动画:真)? }} –

0

我一直在这个确切问题,工作了一段时间,并用此溶液从继承的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)) 
     } 
    } 
} 

编辑:请注意,您仍然需要设置.scaleAspectFit参数,因为这只会改变帧的大小。它保留了完整的图像质量。

+0

@CodeTree对不起,这是一个个人功能,代码现在更新:self.transform = self.transform.scaledBy(x:scale,y:scale) – Simen91

+0

是的,虽然它不会使视图更小它会扩大到更大的一个,这将是非常方便的,我猜在哪里至少我可以玩...我会尽力让你知道。 –

+0

@CodeTree该功能只在视图超过其超视图时才缩小(它不像现在那样扩大)。如果您希望它向上或向下放大或缩小,无论其大小与其超级视图相关,只要移除if语句以检查它是否超出屏幕大小 – Simen91

0

您试过了吗incomingImgView.sizeToFit()此方法适合其父视图的正确大小的视图。

相关问题