Skip to content

Instantly share code, notes, and snippets.

@nathan-fiscaletti
Created November 12, 2018 17:18
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 nathan-fiscaletti/e1b85f68559f305db7342bfff9574563 to your computer and use it in GitHub Desktop.
Save nathan-fiscaletti/e1b85f68559f305db7342bfff9574563 to your computer and use it in GitHub Desktop.
An extension for UIImageView that provides useful tools for handling the scaled size of the image, the padding added by the UIImageView and X/Y coordinate mapping.
//
// UIImageView Additions
//
// Created by Nathan Fiscaletti on 11/12/18.
// Copyright © 2018 Nathan Fiscaletti. All rights reserved.
//
// An extension for UIImageView that provides useful tools for
// handling the scaled size of the image, the padding added by
// the UIImageView and X/Y coordinate mapping.
//
extension UIImageView {
/// Retrieve the scaled size of the image within this ImageView.
///
/// - Returns:
/// - A CGRect representing the size of the image after scaling or nil if no image is set.
///
func getScaledImageSize() -> CGRect? {
if let image = self.image {
return AVMakeRect(aspectRatio: image.size, insideRect: self.frame);
}
return nil;
}
/// Retrieve the size of the image including the padding added by the UIImageView.
///
/// - Returns:
/// - A CGRect reresenting the size including the padding.
///
func getScaledImageSizeWithPadding() -> CGRect? {
if let size = getScaledImageSize(),
let paddingTop = getPaddingTop(),
let paddingBottom = getPaddingBottom(),
let paddingLeft = getPaddingLeft(),
let paddingRight = getPaddingRight() {
return CGRect(x: size.minX + paddingLeft, y: size.minY + paddingTop, width: size.width + paddingLeft + paddingRight, height: size.height + paddingTop + paddingBottom);
}
return nil;
}
/// Retrieve the top padding between the scaled image in this ImageView and
/// the ImageView itself.
///
/// - Returns:
/// - A CGFloat representing the padding, or nil if no image is set.
///
func getPaddingTop() -> CGFloat? {
if let size = self.getScaledImageSize() {
switch self.contentMode {
case .bottom, .bottomLeft, .bottomRight :
return self.frame.height - size.height;
case .top, .topLeft, .topRight :
return 0;
default:
return (self.frame.height - size.height) / 2;
}
}
return nil;
}
/// Retrieve the bottom padding between the scaled image in this ImageView
/// and the ImageView itself.
///
/// - Returns:
/// - A CGFloat representing the padding, or nil if no image is set.
///
func getPaddingBottom() -> CGFloat? {
if let size = self.getScaledImageSize() {
switch self.contentMode {
case .bottom, .bottomLeft, .bottomRight :
return 0;
case .top, .topLeft, .topRight :
return self.frame.height - size.height;
default:
return (self.frame.height - size.height) / 2;
}
}
return nil;
}
/// Retrieve the left padding between the scaled image in this ImageView
/// and the ImageView itself.
///
/// - Returns:
/// - A CGFloat representing the padding, or nil if no image is set.
///
func getPaddingLeft() -> CGFloat? {
if let size = self.getScaledImageSize() {
switch self.contentMode {
case .left, .bottomLeft, .topLeft :
return 0;
case .right, .bottomRight, .topRight :
return self.frame.width - size.width;
default :
if (self.frame.width - size.width) > 0 {
return (self.frame.width - size.width) / 2;
} else {
return 0;
}
}
}
return nil;
}
/// Retrieve the right padding between the scaled image in this ImageView
/// and the ImageView itself.
///
/// - Returns:
/// - A CGFloat representing the padding, or nil if no image is set.
///
func getPaddingRight() -> CGFloat? {
if let size = self.getScaledImageSize() {
switch self.contentMode {
case .left, .bottomLeft, .topLeft :
return self.frame.width - size.width;
case .right, .bottomRight, .topRight :
return 0;
default :
if (self.frame.width - size.width) > 0 {
return (self.frame.width - size.width) / 2;
} else {
return 0;
}
}
}
return nil;
}
/// Retrieve the coordinate from the base image scaled to the new scaled image.
///
/// - Parameters:
/// - x: The X coordinate.
/// - y: The Y coordinate.
/// - multiplier: The multiplier to use (default is 100).
/// - Returns:
/// - A CGPoint representing the value, or nil if no image is set.
///
func getScaledCoordinate(forX x: CGFloat, andY y: CGFloat, multiplier:CGFloat = 100.0) -> CGPoint? {
if let size = self.getScaledImageSize() {
let xCoordSize = size.width / multiplier;
let yCoordSize = size.height / multiplier;
return CGPoint(x: x * xCoordSize, y: y * yCoordSize);
}
return nil;
}
/// Retrieve the coordinate from the base image scaled to the new scaled image,
/// including the padding added by the UIImageView.
///
/// - Parameters:
/// - x: The X coordinate.
/// - y: The Y coordinate.
/// - multiplier: The multiplier to use (default is 100).
/// - Returns:
/// - A CGPoint representing the value, or nil if no image is set.
///
func getScaledCoordinateWithPadding(forX x: CGFloat, andY y: CGFloat, multiplier:CGFloat = 100.0) -> CGPoint? {
if let point = self.getScaledCoordinate(forX: x, andY: y, multiplier: multiplier),
let paddingTop = self.getPaddingTop(),
let paddingLeft = self.getPaddingLeft() {
return CGPoint(x: point.x + paddingLeft, y: point.y + paddingTop);
}
return nil;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment