Created
February 14, 2017 11:09
-
-
Save fozoglu/badf546dcaecff23a32ad417e90f093f to your computer and use it in GitHub Desktop.
frameForImageInImageViewAspectFit metotu ile aspectFit ile değişen boyutu yakalayabilirsin.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension UIImageView{ | |
func frameForImageInImageViewAspectFit() -> CGRect | |
{ | |
if let img = self.image { | |
let imageRatio = img.size.width / img.size.height; | |
let viewRatio = self.frame.size.width / self.frame.size.height; | |
if(imageRatio < viewRatio) | |
{ | |
let scale = self.frame.size.height / img.size.height; | |
let width = scale * img.size.width; | |
let topLeftX = (self.frame.size.width - width) * 0.5; | |
return CGRect(x: topLeftX, y: 0, width: width, height:self.frame.size.height) | |
} | |
else | |
{ | |
let scale = self.frame.size.width / img.size.width; | |
let height = scale * img.size.height; | |
let topLeftY = (self.frame.size.height - height) * 0.5; | |
return CGRect(x: 0, y: topLeftY, width: self.frame.size.width, height:height) | |
} | |
} | |
return CGRect(x: 0, y: 0, width: 0, height: 0) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment