UIImage

public extension UIImage

A set of extensions to the UIImage class. This mostly has resizing stuff, but also a bit of pixel-level inspection.

Class Functions

  • This is a “cascading” image fetcher. It first, ses if there is an asset with the name given, then, it looks in the SFSymbols, finally, returning the SFSymbols.nosign, if none found.

    Declaration

    Swift

    class func assetOrSystemImage(name inName: String) -> UIImage?

    Parameters

    name

    The name of the resource.

    Return Value

    A new image. May be nil, if none found.

Image Composition Instance Computed Properties

  • Declaration

    Swift

    var hasAlphaInformation: Bool { get }

    Return Value

    True, if the image has an alpha component. NOTE: The Photos app seems to have a bug, where it won’t see alpha information of monchrome (black and white) PNG images with alpha channels.

Pixel Information Instance Methods

  • This returns the RGB color (as a UIColor) of the pixel in the image, at the given point. It is restricted to 32-bit (RGBA/8-bit pixel) values.

    This was inspired by several of the answers in this StackOverflow Question.

    NOTE: This is unlikely to be highly performant!

    Declaration

    Swift

    func getRGBColorOfThePixel(at inPoint: CGPoint) -> UIColor?

    Parameters

    at

    The point in the image to sample (NOTE: Must be within image bounds, or nil is returned).

    Return Value

    A UIColor (or nil).

Sizing Instance Methods

  • This allows an image to be resized, given a maximum dimension, and a scale will be determined to meet that dimension. If the image is currently smaller than the maximum size, it will not be scaled.

    Declaration

    Swift

    func resized(toMaximumSize: CGFloat) -> UIImage?

    Parameters

    toMaximumSize

    The maximum size, in either the X or Y axis, of the image, in pixels.

    Return Value

    A new image, with the given dimensions. May be nil, if there was an error.

  • This allows an image to be resized, given a maximum dimension, and a scale will be determined to meet that dimension.

    Declaration

    Swift

    func resized(toScaleFactor inScaleFactor: CGFloat) -> UIImage?

    Parameters

    toScaleFactor

    The scale of the resulting image, as a multiplier of the current size.

    Return Value

    A new image, with the given scale. May be nil, if there was an error.

  • This allows an image to be resized, given both a width and a height, or just one of the dimensions.

    Declaration

    Swift

    func resized(toNewWidth inNewWidth: CGFloat? = nil, toNewHeight inNewHeight: CGFloat? = nil) -> UIImage?

    Parameters

    toNewWidth

    The width (in pixels) of the desired image. If not provided, a scale will be determined from the toNewHeight parameter.

    toNewHeight

    The height (in pixels) of the desired image. If not provided, a scale will be determined from the toNewWidth parameter.

    Return Value

    A new image, with the given dimensions. May be nil, if no width or height was supplied, or if there was an error.