Created
September 3, 2015 14:44
-
-
Save koenpunt/7c6b76f719680a49955d to your computer and use it in GitHub Desktop.
UIImageView extension for calculated UIImageSize
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
// | |
// UIImageView+imageFrame.swift | |
// Tindex | |
// | |
// Created by Koen Punt on 24-08-15. | |
// Copyright (c) 2015 Koen Punt. All rights reserved. | |
// | |
import UIKit | |
/** imageFrame Extends UIImageView | |
*/ | |
extension UIImageView { | |
var imageSize: CGSize { | |
get { | |
if let image: UIImage = self.image { | |
let widthRatio = image.size.width / self.frame.size.width | |
let heightRatio = image.size.height / self.frame.size.height | |
let ratio = maxElement([widthRatio, heightRatio]) | |
return CGSizeMake(image.size.width / ratio, image.size.height / ratio) | |
} | |
return CGSizeZero | |
} | |
} | |
var imageFrame: CGRect { | |
get { | |
if let image: UIImage = self.image { | |
let size = self.imageSize | |
return CGRectMake((self.frame.size.width - size.width) / 2, (self.frame.size.height - size.height) / 2, size.width, size.height) | |
} | |
return CGRectZero | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment