1# ImageSpan 2 3As a child of the [Text](ts-basic-components-text.md) and [ContainerSpan](ts-basic-components-containerspan.md) components, the **ImageSpan** component is used to display inline images. 4 5> **NOTE** 6> 7> This component is supported since API version 10. Updates will be marked with a superscript to indicate their earliest API version. 8 9 10## Child Components 11 12Not supported 13 14 15## APIs 16 17ImageSpan(value: ResourceStr | PixelMap) 18 19**Atomic service API**: This API can be used in atomic services since API version 11. 20 21**System capability**: SystemCapability.ArkUI.ArkUI.Full 22 23**Parameters** 24 25| Name| Type| Mandatory| Description| 26| -------- | -------- | -------- | -------- | 27| value | [ResourceStr](ts-types.md#resourcestr) \| [PixelMap](../../apis-image-kit/js-apis-image.md#pixelmap7) | Yes| Image source. Both local and online images are supported.<br>When using an image referenced using a relative path, for example, **ImageSpan("common/test.jpg")**, the **ImageSpan** component cannot be called across bundles or modules. Therefore, you are advised to use **\$r** to reference image resources that need to be used globally.<br>\- The supported formats include PNG, JPG, BMP, SVG, GIF, and HEIF.<br>\- Base64 strings are supported. The value format is data:image/[png\|jpeg\|bmp\|webp\|heif];base64,[base64 data], where *[base64 data]* is a Base64 string.<br>\- Strings with the **file:///data/storage** prefix are supported, which are used to read image resources in the **files** folder in the installation directory of the application. Ensure that the application has the read permission to the files in the specified path.| 28 29 30## Attributes 31 32The attributes inherit from [BaseSpan](ts-basic-components-span.md#basespan). Among the universal attributes, [size](ts-universal-attributes-size.md#size), [background](ts-universal-attributes-background.md#background), and [border](ts-universal-attributes-border.md#border) are supported. 33 34### verticalAlign 35 36verticalAlign(value: ImageSpanAlignment) 37 38Sets the alignment mode of the image relative to the line height. 39 40**Atomic service API**: This API can be used in atomic services since API version 11. 41 42**System capability**: SystemCapability.ArkUI.ArkUI.Full 43 44**Parameters** 45 46| Name| Type | Mandatory| Description | 47| ------ | ----------------------------------------- | ---- | ------------------------------------------------------------ | 48| value | [ImageSpanAlignment](ts-appendix-enums.md#imagespanalignment10) | Yes | Alignment mode of the image relative to the line height.<br>Default value: **ImageSpanAlignment.BOTTOM**| 49 50### objectFit 51 52objectFit(value: ImageFit) 53 54Sets the image scale type. 55 56**Atomic service API**: This API can be used in atomic services since API version 11. 57 58**System capability**: SystemCapability.ArkUI.ArkUI.Full 59 60**Parameters** 61 62| Name| Type | Mandatory| Description | 63| ------ | ----------------------------------------- | ---- | ------------------------------------------- | 64| value | [ImageFit](ts-appendix-enums.md#imagefit) | Yes | Image scale type.<br>Default value: **ImageFit.Cover**| 65 66### alt<sup>12+</sup> 67 68alt(value: PixelMap) 69 70Sets the placeholder image displayed during loading. 71 72**Atomic service API**: This API can be used in atomic services since API version 12. 73 74**System capability**: SystemCapability.ArkUI.ArkUI.Full 75 76**Parameters** 77 78| Name| Type | Mandatory| Description | 79| ------ | -------------------------------------------------------- | ---- | ------------------------------------------------------------ | 80| value | [PixelMap](../../apis-image-kit/js-apis-image.md#pixelmap7) | Yes | Placeholder image displayed during loading. The [PixelMap](../../apis-image-kit/js-apis-image.md#pixelmap7) type is supported.<br>Default value: **null**| 81 82### colorFilter<sup>14+</sup> 83 84colorFilter(filter: ColorFilter | DrawingColorFilter) 85 86Sets the color filter for the image. 87 88**Atomic service API**: This API can be used in atomic services since API version 14. 89 90**System capability**: SystemCapability.ArkUI.ArkUI.Full 91 92**Parameters** 93 94| Name| Type | Mandatory| Description | 95| ------ | --------------------------------------- | ---- | ------------------------------------------------------------ | 96| filter | [ColorFilter](ts-types.md#colorfilter9) \| [DrawingColorFilter](ts-basic-components-image.md#drawingcolorfilter12) | Yes | 1. Color filter of the image. The input parameter is a 4 x 5 RGBA transformation matrix.<br>The first row of the matrix represents a vector value of R (red), the second row represents a vector value of G (green), the third row represents a vector value of B (blue), and the fourth row represents a vector value of A (alpha). The four rows represent different RGBA vector values.<br>If the matrix contains entries of 1 on the diagonal and entries of 0 in other places, the original color of the image is retained.<br> **Calculation rule:**<br>If the input filter matrix is as follows:<br><br>Wherein the color is [R, G, B, A].<br>Then the color after filtering is [R', G', B', A'].<br><br>2. The ColorFilter type of **@ohos.graphics.drawing** can be used as the input parameter.<br>**NOTE**<br>The DrawingColorfilter type can be used in atomic services. The SVG image to set as the source must have the **stroke** attribute.| 97 98## Events 99 100Among all the universal events, only the [click event](ts-universal-attributes-click.md) is supported. The following events are also supported. 101 102### onComplete<sup>12+</sup> 103 104onComplete(callback: ImageCompleteCallback) 105 106Triggered when the image is successfully loaded or decoded. The size of the loaded image is returned. 107 108**Atomic service API**: This API can be used in atomic services since API version 12. 109 110**System capability**: SystemCapability.ArkUI.ArkUI.Full 111 112**Parameters** 113 114| Name | Type | Mandatory| Description | 115| -------- | ------------------------------------------ | ---- | -------------------------- | 116| callback | [ImageCompleteCallback](#imagecompletecallback12) | Yes | Callback triggered when the image is successfully loaded or decoded.| 117 118### onError<sup>12+</sup> 119 120onError(callback: ImageErrorCallback) 121 122Triggered when an error occurs during image loading. 123 124**Atomic service API**: This API can be used in atomic services since API version 12. 125 126**System capability**: SystemCapability.ArkUI.ArkUI.Full 127 128**Parameters** 129 130| Name | Type | Mandatory| Description | 131| -------- | ------------------------------------------ | ---- | -------------------------- | 132| callback | [ImageErrorCallback](ts-basic-components-image.md#imageerrorcallback9) | Yes | Callback triggered when an error occurs during image loading.| 133 134## ImageCompleteCallback<sup>12+</sup> 135 136type ImageCompleteCallback = (result: ImageLoadResult) => void 137 138Defines the callback triggered when the image is successfully loaded or decoded. 139 140**Atomic service API**: This API can be used in atomic services since API version 12. 141 142**System capability**: SystemCapability.ArkUI.ArkUI.Full 143 144| Name| Type | Mandatory| Description | 145| ------ | -------------------------- | ---- | ---------------------------------- | 146| result | [ImageLoadResult](#imageloadresult12) | Yes | Object returned after the callback is triggered when an image is successfully loaded or decoded.| 147 148## ImageLoadResult<sup>12+</sup> 149 150Object returned after the callback is triggered when an image is successfully loaded or decoded. 151 152**Atomic service API**: This API can be used in atomic services since API version 12. 153 154**System capability**: SystemCapability.ArkUI.ArkUI.Full 155 156| Name | Type | Mandatory| Description | 157| ---------------------------- | ------ | ---- | ------------------------------------------------------------ | 158| width | number | Yes | Width of the image.<br>Unit: pixel | 159| height | number | Yes | Height of the image.<br>Unit: pixel | 160| componentWidth | number | Yes | Width of the component.<br>Unit: pixel | 161| componentHeight | number | Yes | Height of the component.<br>Unit: pixel | 162| loadingStatus | number | Yes | Loading status of the image.<br>**NOTE**<br>If the return value is **0**, the image is successfully loaded. If the return value is **1**, the image is successfully decoded.| 163| contentWidth | number | Yes | Actual rendered width of the image.<br>Unit: pixel<br>**NOTE**<br>This parameter is valid only when the return value of **loadingStatus** is **1**.| 164| contentHeight | number | Yes | Actual rendered height of the image.<br>Unit: pixel<br>**NOTE**<br>This parameter is valid only when the return value of **loadingStatus** is **1**.| 165| contentOffsetX | number | Yes | Offset of the rendered content relative to the component on the x-axis.<br>Unit: pixel<br>**NOTE**<br>This parameter is valid only when the return value of **loadingStatus** is **1**.| 166| contentOffsetY | number | Yes | Offset of the rendered content relative to the component on the y-axis<br>Unit: pixel<br>**NOTE**<br>This parameter is valid only when the return value of **loadingStatus** is **1**.| 167 168 169 170## Example 171 172### Example 1: Setting the Alignment Mode 173 174This example demonstrates the alignment and scaling effects of the **ImageSpan** component using the **verticalAlign** and **objectFit** attributes. 175 176```ts 177// xxx.ets 178@Entry 179@Component 180struct SpanExample { 181 build() { 182 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 183 Text() { 184 Span('This is the Span and ImageSpan component').fontSize(25).textCase(TextCase.Normal) 185 .decoration({ type: TextDecorationType.None, color: Color.Pink }) 186 }.width('100%').textAlign(TextAlign.Center) 187 188 Text() { 189 ImageSpan($r('app.media.app_icon')) 190 .width('200px') 191 .height('200px') 192 .objectFit(ImageFit.Fill) 193 .verticalAlign(ImageSpanAlignment.CENTER) 194 Span('I am LineThrough-span') 195 .decoration({ type: TextDecorationType.LineThrough, color: Color.Red }).fontSize(25) 196 ImageSpan($r('app.media.app_icon')) 197 .width('50px') 198 .height('50px') 199 .verticalAlign(ImageSpanAlignment.TOP) 200 Span('I am Underline-span') 201 .decoration({ type: TextDecorationType.Underline, color: Color.Red }).fontSize(25) 202 ImageSpan($r('app.media.app_icon')) 203 .size({ width: '100px', height: '100px' }) 204 .verticalAlign(ImageSpanAlignment.BASELINE) 205 Span('I am Underline-span') 206 .decoration({ type: TextDecorationType.Underline, color: Color.Red }).fontSize(25) 207 ImageSpan($r('app.media.app_icon')) 208 .width('70px') 209 .height('70px') 210 .verticalAlign(ImageSpanAlignment.BOTTOM) 211 Span('I am Underline-span') 212 .decoration({ type: TextDecorationType.Underline, color: Color.Red }).fontSize(50) 213 } 214 .width('100%') 215 .textIndent(50) 216 }.width('100%').height('100%').padding({ left: 0, right: 0, top: 0 }) 217 } 218} 219``` 220 221 222 223### Example 2: Setting the Background Style 224 225This example demonstrates the effect of setting a background style for text using the **textBackgroundStyle** attribute. 226 227```ts 228// xxx.ets 229@Component 230@Entry 231struct Index { 232 build() { 233 Row() { 234 Column() { 235 Text() { 236 ImageSpan($r('app.media.sky'))// You are advised to use a custom local image. 237 .width('60vp') 238 .height('60vp') 239 .verticalAlign(ImageSpanAlignment.CENTER) 240 .borderRadius(20) 241 .textBackgroundStyle({ color: '#7F007DFF', radius: "5vp" }) 242 } 243 }.width('100%') 244 }.height('100%') 245 } 246} 247``` 248 249 250### Example 3: Adding Events to an Image 251 252This example demonstrates how to add load success and load error events to the **ImageSpan** component using **onComplete** and **onError**. 253 254```ts 255// xxx.ets 256@Entry 257@Component 258struct Index { 259 @State src: ResourceStr = $r('app.media.app_icon'); 260 261 build() { 262 Column() { 263 Text() { 264 ImageSpan(this.src) 265 .width(100).height(100) 266 .onError((err) => { 267 console.log("onError: " + err.message); 268 }) 269 .onComplete((event) => { 270 console.log("onComplete: " + event.loadingStatus); 271 }) 272 } 273 }.width('100%').height('100%') 274 } 275} 276``` 277### Example 4: Setting the Color Filter 278 279This example demonstrates the effect of setting a color filter for the **ImageSpan** component using the **colorFilter** attribute. 280 281```ts 282// xxx.ets 283import { drawing } from '@kit.ArkGraphics2D'; 284 285@Entry 286@Component 287struct SpanExample { 288 private ColorFilterMatrix: number[] = [0.239, 0, 0, 0, 0, 0, 0.616, 0, 0, 0, 0, 0, 0.706, 0, 0, 0, 0, 0, 1, 0]; 289 @State DrawingColorFilterFirst: ColorFilter | undefined = new ColorFilter(this.ColorFilterMatrix); 290 291 build() { 292 Row() { 293 Column({ space: 10 }) { 294 // Use a ColorFilter object to apply a color filter to the image. 295 Text() { 296 ImageSpan($r('app.media.sky'))// You are advised to use a custom local image. 297 .width('60vp') 298 .height('60vp') 299 .colorFilter(this.DrawingColorFilterFirst) 300 } 301 302 // Create a color filter using the drawing.ColorFilter API. 303 Text() { 304 ImageSpan($r('app.media.sky'))// You are advised to use a custom local image. 305 .width('60vp') 306 .height('60vp') 307 .colorFilter(drawing.ColorFilter.createBlendModeColorFilter({ 308 alpha: 255, 309 red: 112, 310 green: 112, 311 blue: 112 312 }, drawing.BlendMode.SRC)) 313 } 314 }.width('100%') 315 }.height('100%') 316 } 317} 318``` 319 320 321### Example 5: Setting a Placeholder Image 322 323This example shows how a placeholder image is used in the **ImageSpan** component during the process of loading an image from the Internet. 324 325```ts 326// xxx.ets 327import { http } from '@kit.NetworkKit'; 328import { image } from '@kit.ImageKit'; 329import { BusinessError } from '@kit.BasicServicesKit'; 330 331@Entry 332@Component 333struct SpanExample { 334 @State imageAlt: PixelMap | undefined = undefined 335 336 httpRequest() { 337 // Enter an image URL. 338 http.createHttp().request("https://www.example.com/xxx.png", (error: BusinessError, data: http.HttpResponse) => { 339 if (error) { 340 console.error(`http request failed with. Code: ${error.code}, message: ${error.message}`); 341 } else { 342 console.log(`http request success.`); 343 let imageData: ArrayBuffer = data.result as ArrayBuffer; 344 let imageSource: image.ImageSource = image.createImageSource(imageData); 345 346 class tmp { 347 height: number = 100; 348 width: number = 100; 349 } 350 351 let option: Record<string, number | boolean | tmp> = { 352 'alphaType': 0, // Alpha type. 353 'editable': false, // Whether the image is editable. 354 'pixelFormat': 3, // Pixel format. 355 'scaleMode': 1, // Scale mode. 356 'size': { height: 100, width: 100 } 357 }; 358 // Image size. 359 imageSource.createPixelMap(option).then((pixelMap: PixelMap) => { 360 this.imageAlt = pixelMap; 361 }) 362 } 363 }) 364 } 365 366 build() { 367 Column() { 368 Button("Get Online Image") 369 .onClick(() => { 370 this.httpRequest(); 371 }) 372 373 Text() { 374 // Enter an image URL. 375 ImageSpan('https://www.example.com/xxx.png') 376 .alt(this.imageAlt) 377 .width(300) 378 .height(300) 379 } 380 381 }.width('100%').height(250).padding({ left: 35, right: 35, top: 35 }) 382 } 383} 384``` 385 386 387