2017-04-13 66 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