Skip to content

Instantly share code, notes, and snippets.

@fozoglu
Created February 14, 2017 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fozoglu/badf546dcaecff23a32ad417e90f093f to your computer and use it in GitHub Desktop.
Save fozoglu/badf546dcaecff23a32ad417e90f093f to your computer and use it in GitHub Desktop.
frameForImageInImageViewAspectFit metotu ile aspectFit ile değişen boyutu yakalayabilirsin.
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