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.

    Note

    The opaqueness is the same of the initial images.

    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 scaled UIImage.

  • Mechanica

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

    Declaration

    Swift

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

    Parameters

    size

    The size to use when scaling the new image.

    opaque

    Specifying false means that the image will include an alpha channel.

    Return Value

    A new scaled UIImage.

  • 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.

  • Mechanica

    Returns a new decoded version of the image.

    It allows a bitmap representation to be decoded in the background rather than on the main thread.

    Declaration

    Swift

    public func decoded(scale: CGFloat? = .none, allowedMaxSize: Int = 4096 * 4096) -> UIImage?

    Parameters

    scale

    The scale factor to assume when interpreting the image data (defaults to self.scale); the decoded image will have its size divided by this scale parameter.

    allowedMaxSize

    The allowed max size, if the image is too large the decoding operation will return nil.

    Return Value

    A new decoded UIImage object.