Image
A view that displays an image.
Declaration
type Image =
| { decorative: string; }
| { decorative: string; variableValue: number | null; }
| { name: string; }
| { name: string; label: string; }
| { name: string; variableValue: number | null; }
| { name: string; variableValue: number | null; label: string; }
| { size: CGSize; label?: string | null; opaque?: boolean; colorMode?: ColorRenderingMode; renderer: RecordedClosure; }
| { systemName: string; }
| { systemName: string; variableValue: number | null; }
| { uiImage: number
| { uri: string; scale?: number; }; }
| string;@frozen struct ImageType methods
Overview
Use an Image instance when you want to add images to your SwiftUI app.
You can create images from many sources:
- Image files in your app's asset library or bundle. Supported types include PNG, JPEG, HEIC, and more.
- Instances of platform-specific image types, like doc://com.apple.documentation/documentation/UIKit/UIImage and doc://com.apple.documentation/documentation/AppKit/NSImage.
- A bitmap stored in a Core Graphics doc://com.apple.documentation/documentation/coregraphics/cgimage instance.
- System graphics from the SF Symbols set.
The following example shows how to load an image from the app's asset library or bundle and scale it to fit within its container:
Image("Landscape_4")
.resizable()
.aspectRatio(contentMode: .fit)
Text("Water wheel")You can use methods on the Image type as well as
standard view modifiers to adjust the size of the image to fit your app's
interface. Here, the Image type's
Image.resizable(capInsets:resizingMode:) method scales the image to fit
the current view. Then, the
View.aspectRatio(_:contentMode:) view modifier adjusts
this resizing behavior to maintain the image's original aspect ratio, rather
than scaling the x- and y-axes independently to fill all four sides of the
view. The article
doc:Fitting-Images-into-Available-Space shows how to apply scaling,
clipping, and tiling to Image instances of different sizes.
An Image is a late-binding token; the system resolves its actual value
only when it's about to use the image in an environment.
Making images accessible
To use an image as a control, use one of the initializers that takes a
label parameter. This allows the system's accessibility frameworks to use
the label as the name of the control for users who use features like
VoiceOver. For images that are only present for aesthetic reasons, use an
initializer with the decorative parameter; the accessibility systems
ignore these images.
From Apple’s SwiftUI documentation. Code samples there are Swift.
Used by
See also
More in Other values.
- AxisThe horizontal or vertical dimension in a 2D coordinate system.
- AxisSetAn efficient set of axes.
- ButtonRoleA value that describes the purpose of a button.
- CapsuleA capsule shape aligned inside the frame of the view containing it.
- CircleA circle centered on the frame of the view containing it.
- ConcentricRectangleA shape whose corners you configure, individually or uniformly, to be squared, rounded, or concentric relative to a container shape's corners.
- ContentMarginGuideA guide for resolving the content margins for a view.
- ContentMarginPlacementThe placement of margins.
- ContentModeConstants that define how a view's content fills the available space.
- DatePickerComponents
- DatePickerComponents2The date and time components that a date picker shows.
- DeviceHingeA type encapsulating the state of a single hinge.