Reference version

This is documentation for the next SDK version. For up-to-date documentation, see the latest version (SDK 56).

图像

一个用于显示 SF Symbols 的 SwiftUI Image 组件。

iOS
tvOS
Included in Expo Go

For the complete documentation index, see llms.txt. Use this file to discover all available pages.

有关跨平台用法,请参阅通用的 Icon — 它会根据平台渲染相应的原生组件。

Expo UI 图像使用 SwiftUI Image API 显示 SF Symbols。SF Symbols 是 Apple 提供的可配置符号库。

一排彩色的 SF Symbol 图像

安装

Terminal
npx expo install @expo/ui

If you are installing this in an existing React Native app, make sure to install expo in your project.

用法

基本 SF Symbol

BasicImageExample.tsx
import { Host, Image } from '@expo/ui/swift-ui'; export default function BasicImageExample() { return ( <Host matchContents> <Image systemName="star.fill" /> </Host> ); }

自定义 SF Symbol

使用 assetName 属性将导入到应用资源目录中的自定义 SF Symbol 作为符号集显示出来。

CustomImageExample.tsx
import { Host, Image } from '@expo/ui/swift-ui'; export default function CustomImageExample() { return ( <Host matchContents> <Image assetName="acme.mark" /> </Host> ); }

带尺寸和颜色

ImageSizeColorExample.tsx
import { Host, HStack, Image } from '@expo/ui/swift-ui'; export default function ImageSizeColorExample() { return ( <Host matchContents> <HStack spacing={16}> <Image systemName="heart.fill" size={24} color="red" /> <Image systemName="star.fill" size={32} color="orange" /> <Image systemName="bell.fill" size={40} color="blue" /> </HStack> </Host> ); }

带可变值

某些 SF Symbols 会根据可变值改变其外观。使用 variableValue 属性并传入 0.0 到 1.0 之间的值来控制渲染出的符号。需要 iOS 16.0+ 和 SF Symbols 4.0+。

ImageVariableExample.tsx
import { Host, HStack, Image } from '@expo/ui/swift-ui'; export default function ImageVariableExample() { return ( <Host matchContents> <HStack spacing={16}> <Image systemName="chart.bar.fill" size={32} variableValue={0.3} /> <Image systemName="chart.bar.fill" size={32} variableValue={0.6} /> <Image systemName="chart.bar.fill" size={32} variableValue={1.0} /> </HStack> </Host> ); }

带符号效果

通过传入来自 @expo/ui/swift-ui/modifierssymbolEffect 修饰符,为符号应用 SF Symbol 效果以实现动画。此效果默认会持续运行。你也可以传入 value 作为离散触发器,使其在每次变化时触发一次;或者传入 isActive 作为布尔开关,在 true 时运行该效果。需要 iOS 17.0 及更高版本。

ImageSymbolEffectExample.tsx
import { Host, Image } from '@expo/ui/swift-ui'; import { symbolEffect } from '@expo/ui/swift-ui/modifiers'; export default function ImageSymbolEffectExample() { return ( <Host matchContents> <Image systemName="wifi" size={48} color="blue" modifiers={[ symbolEffect({ effect: 'variableColor', fillStyle: 'iterative', playbackStyle: 'reversing', }), ]} /> </Host> ); }

下面的示例使用 value 在每次按钮按下时播放 bounce。请从 worklet 中(或通过 scheduleOnUI)向 state.value 写入,以触发该效果。

ImageSymbolEffectValueExample.tsx
import { Button, Host, Image, useNativeState, VStack } from '@expo/ui/swift-ui'; import { symbolEffect } from '@expo/ui/swift-ui/modifiers'; import { scheduleOnUI } from 'react-native-worklets'; export default function ImageSymbolEffectValueExample() { const trigger = useNativeState(0); return ( <Host matchContents> <VStack spacing={16}> <Image systemName="bell.fill" size={48} color="orange" modifiers={[symbolEffect({ effect: 'bounce', direction: 'up' }, { value: trigger })]} /> <Button label="Bounce" onPress={() => scheduleOnUI(() => { 'worklet'; trigger.value = trigger.value + 1; }) } /> </VStack> </Host> ); }

下面的示例使用 isActive 来切换持续的 breathe 动画。

ImageSymbolEffectIsActiveExample.tsx
import { Host, Image, SyncToggle, useNativeState, VStack } from '@expo/ui/swift-ui'; import { symbolEffect } from '@expo/ui/swift-ui/modifiers'; export default function ImageSymbolEffectIsActiveExample() { const isActive = useNativeState(true); return ( <Host matchContents> <VStack spacing={16}> <Image systemName="cloud.fill" size={48} color="cyan" modifiers={[symbolEffect({ effect: 'breathe' }, { isActive })]} /> <SyncToggle label="Breathe" isOn={isActive} /> </VStack> </Host> ); }

API

import { Image } from '@expo/ui/swift-ui';

Component

Image

iOS
tvOS

Type: React.Element<ImageProps>

ImageProps

assetName

iOS
tvOS
Optional • Type: string

The asset catalog name of a custom SF Symbol imported as a symbol set.

color

iOS
tvOS
Optional • Type: ColorValue

The color of the system image. Can be a color name like '#ff00ff', 'red', 'blue', etc.

onPress

iOS
tvOS
Optional • Type: () => void

Callback triggered when the view is pressed.

size

iOS
tvOS
Optional • Type: number

The size of the system image.

systemName

iOS
tvOS
Optional • Type: SFSymbols7_0

The name of the system image (SF Symbol). For example: 'photo', 'heart.fill', 'star.circle'

uiImage

iOS
tvOS
Optional • Type: string

The URI of the local image file to display. For example: 'file:///path/to/image.jpg' Performs a synchronous read operation that blocks the main thread.

variableValue

iOS16.0+
tvOS16.0+
Optional • Type: number

The variable value that alters the symbol's appearance. A number between 0.0 and 1.0. Only works with SF Symbols that support variable values (SF Symbols 4.0+).