UIImage
The UIImage class represents an image object that can be loaded, encoded, converted, and displayed.
It supports creating images from file paths, binary data, or Base64 strings and provides multiple format conversion methods (PNG/JPEG).
UIImage can be displayed directly in an Image component or used with the Data class for storage, uploading, or encryption.
Overview
UIImage is the core class for handling images in the scripting environment. It is commonly used for:
- Loading images from local files, binary data, Base64 strings or networl URL.
- Accessing image dimensions and scale
- Converting image formats (e.g., PNG, JPEG)
- Generating Base64-encoded strings
- Adjusting rendering and resizing modes
- Flipping or tinting images
- Generating thumbnails
- Displaying dynamic images that switch between light and dark modes
Properties
width: number
The width of the image in pixels.
height: number
The height of the image in pixels.
scale: number
The scale factor of the image, usually 1 or 2 for Retina displays.
imageOrientation: string
The orientation of the image. Possible values:
"up""down""left""right""upMirrored""downMirrored""leftMirrored""rightMirrored""unknown"
isSymbolImage: boolean
Indicates whether the image is an SF Symbol image.
renderingMode: "automatic" | "alwaysOriginal" | "alwaysTemplate" | "unknown"
The rendering mode of the image:
automatic: Automatically determined by the systemalwaysOriginal: Display the image in its original coloralwaysTemplate: Render the image as a template (can be tinted)
resizingMode: "tile" | "stretch" | "unknown"
The resizing mode of the image:
"tile": The image is tiled to fill the area"stretch": The image is stretched to fit
capInsets: { top: number, left: number, bottom: number, right: number }
The cap insets that define the stretchable area of the image.
flipsForRightToLeftLayoutDirection: boolean
Indicates whether the image automatically flips in right-to-left (RTL) layout directions.
Instance Methods
preparingThumbnail(size: Size): UIImage | null
Creates a thumbnail with the specified size.
-
Parameters:
size.width: Width of the thumbnailsize.height: Height of the thumbnail
-
Returns: A new
UIImageinstance, ornullif creation fails.
Example:
withBaselineOffset(offset: number): UIImage
Returns a new image with the specified baseline offset, it is useful for aligning text to the bottom of an image.
withHorizontallyFlippedOrientation(): UIImage
Returns a new image with horizontally flipped orientation.
withTintColor(color: string, renderingMode?: "automatic" | "alwaysOriginal" | "alwaysTemplate"): UIImage | null
Applies a tint color to the image using the specified rendering mode.
-
Parameters:
color: The tint color as a string, e.g."#ffcc00"or"rgb(255,128,0)"renderingMode: The rendering mode to use. Defaults to"automatic"
Example:
withRenderingMode(renderingMode: "automatic" | "alwaysOriginal" | "alwaysTemplate"): UIImage | null
Returns a new version of the image using the specified rendering mode.
resizableImage(capInsets, resizingMode?): UIImage | null
Returns a new version of the image with specified cap insets and resizing mode.
-
Parameters:
capInsets:{ top, left, bottom, right }resizingMode:"tile"or"stretch"(default"tile")
Example:
renderedInCircle(radius?: number | null, fitEntireImage?: boolean): UIImage
Returns a new version of the image rendered as a circle, with optional radius and fit behavior.
-
Parameters:
-
radius(optional): The radius of the circle in points.-
If not specified:
- When
fitEntireImageisfalse, the circle uses the shortest dimension of the image. - When
fitEntireImageistrue, the circle uses the longest dimension of the image.
- When
-
-
fitEntireImage(optional): Whether to fit the entire image inside the circle.- Default value:
true. - If set to
false, the image fills the circular area and may be cropped.
- Default value:
-
-
Returns:
- A new
UIImageinstance representing the circular rendering.
- A new
Example 1: Create a default circular avatar
Example 2: Specify radius and fit entire image
Example 3: Fill mode (may crop parts of the image)
renderedIn(size: { width: number, height: number }, source?: { position?: ..., size?: ... }): UIImage | null
Returns a new version of the image rendered with the specified size and optional source region.
-
Parameters:
size:{ width: number, height: number }- The size of the rendered image.source:{ position?: { x: number, y: number }, size?: { width: number, height: number } }- The source region of the image.
-
Returns:
- A new
UIImageinstance if rendering succeeds, ornullif it fails.
- A new
Example 1: Scale entire image into a rectangle
Example 2: Crop and render a specific region
applySymbolConfiguration(config: UIImageSymbolConfiguration | UIImageSymbolConfiguration[]): UIImage | null
Returns a new version of the image with the specified symbol configuration (UIImageSymbolConfiguration).
This method is primarily used to customize the appearance of SF Symbols images, including color, weight, size, rendering mode, and palette.
-
Parameters:
-
config: The symbol configuration to apply to the image.- Can be a single
UIImageSymbolConfigurationinstance, or - An array of configurations, which will be applied sequentially (later configurations may override earlier ones).
- Can be a single
-
-
Returns:
- A new
UIImageinstance representing the symbol with applied configuration, ornullif the operation fails.
- A new
Example 1: Display a symbol in multicolor
Example 2: Apply both scale and weight configurations
Example 3: Set hierarchical and palette colors
UIImageSymbolConfiguration
UIImageSymbolConfiguration is a class used to define appearance configurations for symbol images (SF Symbols).
Instances created with its static methods can be passed to applySymbolConfiguration() to modify how the symbol is rendered.
Available Static Methods
Example: Combine multiple symbol configurations
toJPEGData(compressionQuality?: number): Data | null
Converts the image to JPEG data.
-
Parameters:
compressionQuality: A number between0and1, default is1.
-
Returns: A
Datainstance ornull.
toPNGData(): Data | null
Converts the image to PNG data.
Returns a Data instance or null.
toJPEGBase64String(compressionQuality?: number): string | null
Converts the image to a Base64-encoded JPEG string.
toPNGBase64String(): string | null
Converts the image to a Base64-encoded PNG string.
Static Methods
UIImage.fromData(data: Data): UIImage | null
Creates an image from a Data instance containing PNG or JPEG data.
UIImage.fromFile(filePath: string): UIImage | null
Creates an image from a file path (supports PNG, JPG, JPEG).
UIImage.fromBase64String(base64String: string): UIImage | null
Creates an image from a Base64 string.
UIImage.fromSFSymbol(name: string): UIImage | null
Creates an image from a system SF Symbol.
Example:
UIImage.fromURL(url: string): Promise<UIImage | null>
Creates an image from a URL.
Example:
Using UIImage in the UI
UIImage can be displayed directly in the <Image> component.
Component Definition
Props Definition
Dynamic Image Type
Used for automatically switching images in light and dark mode.
Example: Display a Single Image
Example: Light and Dark Mode
Common Usage Examples
1. Convert Image to Base64
2. Compress and Save as JPEG
3. Restore Image from Base64 String
4. Convert PNG to JPEG and Upload
5. Create and Tint an SF Symbol
6. Generate a Thumbnail
Summary
UIImage is the core class for image manipulation in the Scripting environment. It provides:
- Loading from files, binary data, or Base64 strings
- Support for SF Symbols
- Access to dimensions, scale, orientation, and rendering information
- Methods for flipping, tinting, and resizing
- PNG/JPEG conversion and Base64 encoding
- Thumbnail generation and customizable rendering modes
- Seamless integration with the
<Image>component - Light and dark mode image switching support
