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>13+</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 13. 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 173 174This example sets the basic attributes of the image span as well as the alignment mode of the image with the text. 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 224 225This example shows how to set the background style of an image span. 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 248This example implements a callback triggered when the image is successfully loaded or decoded and a callback triggered when an error occurs during image loading. 249```ts 250// xxx.ets 251@Entry 252@Component 253struct Index { 254 @State src: ResourceStr = $r('app.media.icon'); 255 build(){ 256 Column(){ 257 Text(){ 258 ImageSpan(this.src) 259 .width(100).height(100) 260 .onError((err)=>{ 261 console.log("onError:" + err.message) 262 }) 263 .onComplete((event)=>{ 264 console.log("onComplete: " + event.loadingStatus) 265 }) 266 } 267 }.width('100%').height('100%') 268 } 269} 270``` 271### Example 4 272 273This example sets the color filter of the image span. 274 275```ts 276// xxx.ets 277import { drawing, common2D } from '@kit.ArkGraphics2D'; 278 279@Entry 280@Component 281struct SpanExample { 282 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]; 283 @State DrawingColorFilterFirst: ColorFilter | undefined = new ColorFilter(this.ColorFilterMatrix) 284 285 build() { 286 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 287 Text() { 288 ImageSpan($r('app.media.icon')) 289 .width('50px') 290 .height('50px') 291 .colorFilter(this.DrawingColorFilterFirst) 292 } 293 .width('50%') 294 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 295 Text() { 296 ImageSpan($r('app.media.icon')) 297 .width('50px') 298 .height('50px') 299 .colorFilter(drawing.ColorFilter.createBlendModeColorFilter({ alpha: 255, red: 112, green: 112, blue: 112 }, drawing.BlendMode.SRC)) 300 } 301 .width('50%') 302 }.width('100%').height('10%') 303 }.width('200%').height('100%') 304 } 305} 306``` 307 308 309### Example 5 310 311This example shows how a placeholder image is used in **ImageSpan** during the process of loading an image from the Internet. 312 313```ts 314// xxx.ets 315import { http } from '@kit.NetworkKit' 316import { image } from '@kit.ImageKit' 317import { BusinessError } from '@kit.BasicServicesKit' 318 319@Entry 320@Component 321struct SpanExample { 322 @State imageAlt: PixelMap | undefined = undefined 323 324 httpRequest() { 325 // Enter an image URL. 326 http.createHttp().request("https://www.example.com/xxx.png", (error: BusinessError, data: http.HttpResponse) => { 327 if (error) { 328 console.error(`http request failed with. Code: ${error.code}, message: ${error.message}`) 329 } else { 330 console.log(`http request success.`) 331 let imageData: ArrayBuffer = data.result as ArrayBuffer 332 let imageSource: image.ImageSource = image.createImageSource(imageData) 333 334 class tmp { 335 height: number = 100 336 width: number = 100 337 } 338 339 let option: Record<string, number | boolean | tmp> = { 340 'alphaType': 0, // Alpha type. 341 'editable': false, // Whether the image is editable. 342 'pixelFormat': 3, // Pixel format. 343 'scaleMode': 1, // Scale mode. 344 'size': { height: 100, width: 100 } 345 } 346 // Image size. 347 imageSource.createPixelMap(option).then((pixelMap: PixelMap) => { 348 this.imageAlt = pixelMap 349 }) 350 } 351 }) 352 } 353 354 build() { 355 Column() { 356 Button("Get Online Image") 357 .onClick(() => { 358 this.httpRequest() 359 }) 360 361 Text() { 362 // Enter an image URL. 363 ImageSpan('https://www.example.com/xxx.png') 364 .alt(this.imageAlt) 365 .width(300) 366 .height(300) 367 } 368 369 }.width('100%').height(250).padding({ left: 35, right: 35, top: 35 }) 370 } 371} 372``` 373 374 375