UIImage

public extension UIImage
  • SwifterSwift: Size in bytes of UIImage

    Declaration

    Swift

    var bytesSize: Int { get }
  • SwifterSwift: Size in kilo bytes of UIImage

    Declaration

    Swift

    var kilobytesSize: Int { get }
  • SwifterSwift: UIImage with .alwaysOriginal rendering mode.

    Declaration

    Swift

    var original: UIImage { get }
  • SwifterSwift: UIImage with .alwaysTemplate rendering mode.

    Declaration

    Swift

    var template: UIImage { get }

Methods

  • SwifterSwift: Compressed UIImage from original UIImage.

    Declaration

    Swift

    func compressed(quality: CGFloat = 0.5) -> UIImage?

    Parameters

    quality

    The quality of the resulting JPEG image, expressed as a value from 0.0 to 1.0. The value 0.0 represents the maximum compression (or lowest quality) while the value 1.0 represents the least compression (or best quality), (default is 0.5).

    Return Value

    optional UIImage (if applicable).

  • SwifterSwift: Compressed UIImage data from original UIImage.

    Declaration

    Swift

    func compressedData(quality: CGFloat = 0.5) -> Data?

    Parameters

    quality

    The quality of the resulting JPEG image, expressed as a value from 0.0 to 1.0. The value 0.0 represents the maximum compression (or lowest quality) while the value 1.0 represents the least compression (or best quality), (default is 0.5).

    Return Value

    optional Data (if applicable).

  • SwifterSwift: UIImage Cropped to CGRect.

    Declaration

    Swift

    func cropped(to rect: CGRect) -> UIImage

    Parameters

    rect

    CGRect to crop UIImage to.

    Return Value

    cropped UIImage

  • SwifterSwift: UIImage scaled to height with respect to aspect ratio.

    Declaration

    Swift

    func scaled(toHeight: CGFloat, opaque: Bool = false) -> UIImage?

    Parameters

    toHeight

    new height.

    opaque

    flag indicating whether the bitmap is opaque.

    Return Value

    optional scaled UIImage (if applicable).

  • SwifterSwift: UIImage scaled to width with respect to aspect ratio.

    Declaration

    Swift

    func scaled(toWidth: CGFloat, opaque: Bool = false) -> UIImage?

    Parameters

    toWidth

    new width.

    opaque

    flag indicating whether the bitmap is opaque.

    Return Value

    optional scaled UIImage (if applicable).

  • SwifterSwift: Creates a copy of the receiver rotated by the given angle.

    // Rotate the image by 180°
    image.rotated(by: Measurement(value: 180, unit: .degrees))
    

    Declaration

    Swift

    @available(tvOS 10.0, watchOS 3.0, *)
    func rotated(by angle: Measurement<UnitAngle>) -> UIImage?

    Parameters

    angle

    The angle measurement by which to rotate the image.

    Return Value

    A new image rotated by the given angle.

  • SwifterSwift: Creates a copy of the receiver rotated by the given angle (in radians).

    // Rotate the image by 180°
    image.rotated(by: .pi)
    

    Declaration

    Swift

    func rotated(by radians: CGFloat) -> UIImage?

    Parameters

    radians

    The angle, in radians, by which to rotate the image.

    Return Value

    A new image rotated by the given angle.

  • SwifterSwift: UIImage filled with color

    Declaration

    Swift

    func filled(withColor color: UIColor) -> UIImage

    Parameters

    color

    color to fill image with.

    Return Value

    UIImage filled with given color.

  • SwifterSwift: UIImage tinted with color

    Declaration

    Swift

    func tint(_ color: UIColor, blendMode: CGBlendMode, alpha: CGFloat = 1.0) -> UIImage

    Parameters

    color

    color to tint image with.

    blendMode

    how to blend the tint

    Return Value

    UIImage tinted with given color.

  • SwifterSwift: UImage with background color

    Declaration

    Swift

    func withBackgroundColor(_ backgroundColor: UIColor) -> UIImage

    Parameters

    backgroundColor

    Color to use as background color

    Return Value

    UIImage with a background color that is visible where alpha < 1

  • SwifterSwift: UIImage with rounded corners

    Declaration

    Swift

    func withRoundedCorners(radius: CGFloat? = nil) -> UIImage?

    Parameters

    radius

    corner radius (optional), resulting image will be round if unspecified

    Return Value

    UIImage with all corners rounded

  • SwifterSwift: Base 64 encoded PNG data of the image.

    Declaration

    Swift

    func pngBase64String() -> String?

    Return Value

    Base 64 encoded PNG data of the image as a String.

  • SwifterSwift: Base 64 encoded JPEG data of the image.

    Declaration

    Swift

    func jpegBase64String(compressionQuality: CGFloat) -> String?

    Parameters

    compressionQuality

    The quality of the resulting JPEG image, expressed as a value from 0.0 to 1.0. The value 0.0 represents the maximum compression (or lowest quality) while the value 1.0 represents the least compression (or best quality).

    Return Value

    Base 64 encoded JPEG data of the image as a String.

Initializers

  • SwifterSwift: Create UIImage from color and size.

    Declaration

    Swift

    convenience init(color: UIColor, size: CGSize)

    Parameters

    color

    image fill color.

    size

    image size.

  • SwifterSwift: Create a new image from a base 64 string.

    Declaration

    Swift

    convenience init?(base64String: String, scale: CGFloat = 1.0)

    Parameters

    base64String

    a base-64 String, representing the image

    scale

    The scale factor to assume when interpreting the image data created from the base-64 string. Applying a scale factor of 1.0 results in an image whose size matches the pixel-based dimensions of the image. Applying a different scale factor changes the size of the image as reported by the size property.

  • SwifterSwift: Create a new image from a URL

    Important

    Use this method to convert data:// URLs to UIImage objects. Don’t use this synchronous initializer to request network-based URLs. For network-based URLs, this method can block the current thread for tens of seconds on a slow network, resulting in a poor user experience, and in iOS, may cause your app to be terminated. Instead, for non-file URLs, consider using this in an asynchronous way, using dataTask(with:completionHandler:) method of the URLSession class or a library such as AlamofireImage, Kingfisher, SDWebImage, or others to perform asynchronous network image loading.

    Declaration

    Swift

    convenience init?(url: URL, scale: CGFloat = 1.0) throws

    Parameters

    url

    a URL, representing the image location

    scale

    The scale factor to assume when interpreting the image data created from the URL. Applying a scale factor of 1.0 results in an image whose size matches the pixel-based dimensions of the image. Applying a different scale factor changes the size of the image as reported by the size property.