Skip to content

Instantly share code, notes, and snippets.

@koenpunt
Created September 3, 2015 14:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koenpunt/7c6b76f719680a49955d to your computer and use it in GitHub Desktop.
Save koenpunt/7c6b76f719680a49955d to your computer and use it in GitHub Desktop.
UIImageView extension for calculated UIImageSize
//
// 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