UIImage

class UIImage : NSObject, NSSecureCoding
  • Mechanica

    Returns an image with a background color, size and scale.

    Note

    The size of the rectangle is beeing rounded from UIKit.

    Declaration

    Swift

    public convenience init?(color: UIColor, size: CGSize = CGSize(width: 1, height: 1), scale scaleFactor: CGFloat = 0.0)

    Parameters

    color

    The background UIColor.

    size

    The image size (default is 1x1).

    scaleFactor

    The scale factor to apply; if you specify a value of 0.0, the scale factor is set to the scale factor of the device’s main screen.

  • Mechanica

    Returns a new version of the image scaled to the specified size.

    Declaration

    Swift

    public func scaled(to size: CGSize, scale scaleFactor: CGFloat = 0.0) -> UIImage

    Parameters

    size

    The size to use when scaling the new image.

    Return Value

    A new image object.

  • Mechanica

    Returns a new version of the image scaled from the center while maintaining the aspect ratio to fit within a specified size.

    Declaration

    Swift

    public func aspectScaled(toFit size: CGSize, scale scaleFactor: CGFloat = 0.0) -> UIImage

    Parameters

    size

    The size to use when scaling the new image.

    scaleFactor

    The display scale of the image renderer context (defaults to the scale of the main screen).

    Return Value

    A new image object.

  • Mechanica

    Returns a new version of the image scaled from the center while maintaining the aspect ratio to fill a specified size. Any pixels that fall outside the specified size are clipped.

    Declaration

    Swift

    public func aspectScaled(toFill size: CGSize, scale scaleFactor: CGFloat = 0.0) -> UIImage

    Parameters

    size

    The size to use when scaling the new image.

    scaleFactor

    The display scale of the image renderer context (defaults to the scale of the main screen).

    Return Value

    A new UIImage object.

  • Mechanica

    Returns a new version of the image with the corners rounded to the specified radius.

    Declaration

    Swift

    public func rounded(withCornerRadius radius: CGFloat, divideRadiusByImageScale: Bool = false, scale scaleFactor: CGFloat = 0.0) -> UIImage

    Parameters

    radius

    The radius to use when rounding the new image.

    divideRadiusByImageScale

    Whether to divide the radius by the image scale. Set to true when the image has the same resolution for all screen scales such as @1x, @2x and @3x (i.e. single image from web server). Set to false for images loaded from an asset catalog with varying resolutions for each screen scale. false by default.

    scaleFactor

    The display scale of the image renderer context (defaults to the scale of the main screen).

    Return Value

    A new UIImage object.

  • Mechanica

    Returns a new version of the image rounded into a circle.

    Declaration

    Swift

    public func roundedIntoCircle(scale scaleFactor: CGFloat = 0.0) -> UIImage

    Parameters

    scaleFactor

    The display scale of the image renderer context (defaults to the scale of the main screen).

    Return Value

    A new UIImage object.