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.
-
This convenience initializer allows us to create a solid rectangular image, of just one color.
Declaration
Swift
convenience init?(color inColor: UIColor, size inSize: CGSize = CGSize(width: 1, height: 1))
Parameters
color
The color to use as a fill.
size
The size of the image. This is optional. If not provided, the image will be 1-pixel square.
Return Value
A new instance (or nil) of an image filled with the color.
-
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.
-
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.
-
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).
-
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.