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### alt<sup>12+</sup> 35 36alt(value: PixelMap) 37 38Sets the placeholder image displayed during loading. 39 40**Atomic service API**: This API can be used in atomic services since API version 12. 41 42**System capability**: SystemCapability.ArkUI.ArkUI.Full 43 44**Parameters** 45 46| Name| Type | Mandatory| Description | 47| ------ | -------------------------------------------------------- | ---- | ------------------------------------------------------------ | 48| 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**| 49 50### verticalAlign 51 52verticalAlign(value: ImageSpanAlignment) 53 54Sets the alignment mode of the image relative to the line height. 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 | [ImageSpanAlignment](ts-appendix-enums.md#imagespanalignment10) | Yes | Alignment mode of the image relative to the line height.<br>Default value: **ImageSpanAlignment.BOTTOM**| 65 66### objectFit 67 68objectFit(value: ImageFit) 69 70Sets the image scale type. 71 72**Atomic service API**: This API can be used in atomic services since API version 11. 73 74**System capability**: SystemCapability.ArkUI.ArkUI.Full 75 76**Parameters** 77 78| Name| Type | Mandatory| Description | 79| ------ | ----------------------------------------- | ---- | ------------------------------------------- | 80| value | [ImageFit](ts-appendix-enums.md#imagefit) | Yes | Image scale type.<br>Default value: **ImageFit.Cover**| 81 82### colorFilter<sup>14+</sup> 83 84colorFilter(value: 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| value | [ColorFilter](ts-types.md#colorfilter9) \| [DrawingColorFilter](../../apis-arkgraphics2d/js-apis-graphics-drawing.md#colorfilter) | 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 138Triggered when the image is successfully loaded. 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.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.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.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.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 Column() { 234 Text() { 235 ImageSpan($r('app.media.app_icon')) 236 .width('60vp') 237 .height('60vp') 238 .verticalAlign(ImageSpanAlignment.CENTER) 239 .textBackgroundStyle({color: Color.Green, radius: "5vp"}) 240 } 241 }.width('100%').alignItems(HorizontalAlign.Center) 242 } 243} 244``` 245 246 247### Example 3: Adding Events to an Image 248 249This example demonstrates how to add load success and load error events to the **ImageSpan** component using **onComplete** and **onError**. 250 251```ts 252// xxx.ets 253@Entry 254@Component 255struct Index { 256 @State src: ResourceStr = $r('app.media.icon'); 257 build(){ 258 Column(){ 259 Text(){ 260 ImageSpan(this.src) 261 .width(100).height(100) 262 .onError((err)=>{ 263 console.log("onError:" + err.message) 264 }) 265 .onComplete((event)=>{ 266 console.log("onComplete: " + event.loadingStatus) 267 }) 268 } 269 }.width('100%').height('100%') 270 } 271} 272``` 273### Example 4: Setting the Color Filter 274 275This example demonstrates the effect of setting a color filter for the **ImageSpan** component using the **colorFilter** attribute. 276 277```ts 278// xxx.ets 279import { drawing, common2D } from '@kit.ArkGraphics2D'; 280 281@Entry 282@Component 283struct SpanExample { 284 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]; 285 @State DrawingColorFilterFirst: ColorFilter | undefined = new ColorFilter(this.ColorFilterMatrix) 286 287 build() { 288 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 289 Text() { 290 ImageSpan($r('app.media.icon')) 291 .width('50px') 292 .height('50px') 293 .colorFilter(this.DrawingColorFilterFirst) 294 } 295 .width('50%') 296 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 297 Text() { 298 ImageSpan($r('app.media.icon')) 299 .width('50px') 300 .height('50px') 301 .colorFilter(drawing.ColorFilter.createBlendModeColorFilter({ alpha: 255, red: 112, green: 112, blue: 112 }, drawing.BlendMode.SRC)) 302 } 303 .width('50%') 304 }.width('100%').height('10%') 305 }.width('200%').height('100%') 306 } 307} 308``` 309 310 311### Example 5: Setting a Placeholder Image 312 313This example shows how a placeholder image is used in the **ImageSpan** component during the process of loading an image from the Internet. 314 315```ts 316// xxx.ets 317import { http } from '@kit.NetworkKit' 318import { image } from '@kit.ImageKit' 319import { BusinessError } from '@kit.BasicServicesKit' 320 321@Entry 322@Component 323struct SpanExample { 324 @State imageAlt: PixelMap | undefined = undefined 325 326 httpRequest() { 327 // Enter an image URL. 328 http.createHttp().request("https://www.example.com/xxx.png", (error: BusinessError, data: http.HttpResponse) => { 329 if (error) { 330 console.error(`http request failed with. Code: ${error.code}, message: ${error.message}`) 331 } else { 332 console.log(`http request success.`) 333 let imageData: ArrayBuffer = data.result as ArrayBuffer 334 let imageSource: image.ImageSource = image.createImageSource(imageData) 335 336 class tmp { 337 height: number = 100 338 width: number = 100 339 } 340 341 let option: Record<string, number | boolean | tmp> = { 342 'alphaType': 0, // Alpha type. 343 'editable': false, // Whether the image is editable. 344 'pixelFormat': 3, // Pixel format. 345 'scaleMode': 1, // Scale mode. 346 'size': { height: 100, width: 100 } 347 } 348 // Image size. 349 imageSource.createPixelMap(option).then((pixelMap: PixelMap) => { 350 this.imageAlt = pixelMap 351 }) 352 } 353 }) 354 } 355 356 build() { 357 Column() { 358 Button("Get Online Image") 359 .onClick(() => { 360 this.httpRequest() 361 }) 362 363 Text() { 364 // Enter an image URL. 365 ImageSpan('https://www.example.com/xxx.png') 366 .alt(this.imageAlt) 367 .width(300) 368 .height(300) 369 } 370 371 }.width('100%').height(250).padding({ left: 35, right: 35, top: 35 }) 372 } 373} 374``` 375 376 377