2017-04-13 62 views
1

即时通讯目前与编辑核心显卡获取的uImage的大小时的UIImageView时ContentMode = AspectFill

当我在图像上得出的UIImage的内容模式设置为.aspectFill

一个的UIImage我想保留图像的比例/比例。

伊夫尝试下面的代码,但它返回.aspectFit值而不是.AspectFill

let imageSize = AVMakeRect(aspectRatio: (mainImageView.image?.size)!, insideRect: mainImageView.frame) 

任何帮助,将不胜感激

托马斯

回答

1

这个功能只能做一个方面的配合。

这里是一个会做一个方面填充功能:

func makeFillRect(aspectRatio: CGSize, insideRect: CGRect) -> CGRect { 
    let aspectRatioFraction = aspectRatio.width/aspectRatio.height 
    let insideRectFraction = insideRect.size.width/insideRect.size.height 
    let r: CGRect 
    if (aspectRatioFraction > insideRectFraction) { 
     let w = insideRect.size.height * aspectRatioFraction 
     r = CGRect(x: (insideRect.size.width - w)/2, y: 0, width: w, height: insideRect.size.height) 
    } else { 
     let h = insideRect.size.width/aspectRatioFraction 
     r = CGRect(x: 0, y: (insideRect.size.height - h)/2, width: insideRect.size.width, height: h) 
    } 
    return r 
} 
+0

非常感谢你 – Tom