1e41f4b71Sopenharmony_ci# Image Display (Image) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ciMore often than not, you may need to display images in your application, for example, icons in buttons, online images, and local images. This is where the **Image** component comes in handy. The **Image** component supports a wide range of image formats, including PNG, JPG, BMP, SVG, and GIF. For details, see [Image](../reference/apis-arkui/arkui-ts/ts-basic-components-image.md). 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciTo use the **Image** component, call the following API: 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci```ts 10e41f4b71Sopenharmony_ciImage(src: PixelMap | ResourceStr | DrawableDescriptor) 11e41f4b71Sopenharmony_ci``` 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ciThis API obtains a local or online image from the data source specified by **src**. For details about how to load the data source, see [Loading Image Resources](#loading-image-resources). 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci## Loading Image Resources 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ciThe **Image** component supports two types of images: archived and pixel map. 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci### Archived Type Data Sources 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ciData sources of the archived type can be classified into local resources, online resources, **Resource** objects, media library resources, and Base64 resources. 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci- Local resources 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci To load local images, create an **ets** folder and place the local images in any position in the folder. 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci Then, in the **Image** component, set **src** to the local image path, with the root directory being the **ets** folder. 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci ```ts 33e41f4b71Sopenharmony_ci Image('images/view.jpg') 34e41f4b71Sopenharmony_ci .width(200) 35e41f4b71Sopenharmony_ci ``` 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci- Online resources 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci To use online images, first apply for the **ohos.permission.INTERNET** permission. For details, see [Declaring Permissions](../security/AccessToken/declare-permissions.md). Then, in the **Image** component, set **src** to the URL of the online image. 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci Currently, the **Image** component supports only simple online images. 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci If an online image has been loaded before, the **Image** component can obtain it from the cache, instead of having to request it from the Internet again. For details about the image cache, see [setImageCacheCount](../reference/apis-arkui/js-apis-system-app.md#setimagecachecount7), [setImageRawDataCacheSize](../reference/apis-arkui/js-apis-system-app.md#setimagerawdatacachesize7), and [setImageFileCacheSize](../reference/apis-arkui/js-apis-system-app.md#setimagefilecachesize7). It should be noted that these image caching APIs are not flexible and will not be further developed. For complex scenarios, you are advised to use [ImageKnife](https://gitee.com/openharmony-tpc/ImageKnife). 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci ```ts 46e41f4b71Sopenharmony_ci Image('https://www.example.com/example.JPG') // Replace the URL with the actual URL. 47e41f4b71Sopenharmony_ci ``` 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci- **Resource** objects 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci **Resource** objects can be used to import images across bundles and modules. To load **Resource** objects, place images in the **resources** folder, which can be read and converted to the **Resource** objects through **$r**. 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci **Figure 1** resources 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci  56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci API: 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci ``` 60e41f4b71Sopenharmony_ci Image($r('app.media.icon')) 61e41f4b71Sopenharmony_ci ``` 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci You can also place the images in the **rawfile** folder. 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci **Figure 2** rawfile folder 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ci  68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci API: 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci ``` 72e41f4b71Sopenharmony_ci Image($rawfile('example1.png')) 73e41f4b71Sopenharmony_ci ``` 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ci- Media library **file://data/storage** 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ci To load images from the [media library](../reference/apis-core-file-kit/js-apis-file-picker.md), use a path string that starts with **file://**. 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ci 1. Call the API to obtain the image URL in the media library. 80e41f4b71Sopenharmony_ci ```ts 81e41f4b71Sopenharmony_ci import { photoAccessHelper } from '@kit.MediaLibraryKit'; 82e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci @Entry 85e41f4b71Sopenharmony_ci @Component 86e41f4b71Sopenharmony_ci struct Index { 87e41f4b71Sopenharmony_ci @State imgDatas: string[] = []; 88e41f4b71Sopenharmony_ci // Obtain the image URL set. 89e41f4b71Sopenharmony_ci getAllImg() { 90e41f4b71Sopenharmony_ci try { 91e41f4b71Sopenharmony_ci let PhotoSelectOptions:photoAccessHelper.PhotoSelectOptions = new photoAccessHelper.PhotoSelectOptions(); 92e41f4b71Sopenharmony_ci PhotoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE; 93e41f4b71Sopenharmony_ci PhotoSelectOptions.maxSelectNumber = 5; 94e41f4b71Sopenharmony_ci let photoPicker:photoAccessHelper.PhotoViewPicker = new photoAccessHelper.PhotoViewPicker(); 95e41f4b71Sopenharmony_ci photoPicker.select(PhotoSelectOptions).then((PhotoSelectResult:photoAccessHelper.PhotoSelectResult) => { 96e41f4b71Sopenharmony_ci this.imgDatas = PhotoSelectResult.photoUris; 97e41f4b71Sopenharmony_ci console.info('PhotoViewPicker.select successfully, PhotoSelectResult uri: ' + JSON.stringify(PhotoSelectResult)); 98e41f4b71Sopenharmony_ci }).catch((err:Error) => { 99e41f4b71Sopenharmony_ci let message = (err as BusinessError).message; 100e41f4b71Sopenharmony_ci let code = (err as BusinessError).code; 101e41f4b71Sopenharmony_ci console.error(`PhotoViewPicker.select failed with. Code: ${code}, message: ${message}`); 102e41f4b71Sopenharmony_ci }); 103e41f4b71Sopenharmony_ci } catch (err) { 104e41f4b71Sopenharmony_ci let message = (err as BusinessError).message; 105e41f4b71Sopenharmony_ci let code = (err as BusinessError).code; 106e41f4b71Sopenharmony_ci console.error(`PhotoViewPicker failed with. Code: ${code}, message: ${message}`); } 107e41f4b71Sopenharmony_ci } 108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_ci // Call the preceding function in aboutToAppear to obtain the image URL set and store the URLs in imgDatas. 110e41f4b71Sopenharmony_ci async aboutToAppear() { 111e41f4b71Sopenharmony_ci this.getAllImg(); 112e41f4b71Sopenharmony_ci } 113e41f4b71Sopenharmony_ci // Use the URL of imgDatas to load the image. 114e41f4b71Sopenharmony_ci build() { 115e41f4b71Sopenharmony_ci Column() { 116e41f4b71Sopenharmony_ci Grid() { 117e41f4b71Sopenharmony_ci ForEach(this.imgDatas, (item:string) => { 118e41f4b71Sopenharmony_ci GridItem() { 119e41f4b71Sopenharmony_ci Image(item) 120e41f4b71Sopenharmony_ci .width(200) 121e41f4b71Sopenharmony_ci } 122e41f4b71Sopenharmony_ci }, (item:string):string => JSON.stringify(item)) 123e41f4b71Sopenharmony_ci } 124e41f4b71Sopenharmony_ci }.width('100%').height('100%') 125e41f4b71Sopenharmony_ci } 126e41f4b71Sopenharmony_ci } 127e41f4b71Sopenharmony_ci ``` 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ci2. Check the format of the URL obtained from the media library: 130e41f4b71Sopenharmony_ci ```ts 131e41f4b71Sopenharmony_ci Image('file://media/Photos/5') 132e41f4b71Sopenharmony_ci .width(200) 133e41f4b71Sopenharmony_ci ``` 134e41f4b71Sopenharmony_ci 135e41f4b71Sopenharmony_ci- Base64 136e41f4b71Sopenharmony_ci 137e41f4b71Sopenharmony_ci As shown above, the URL format is data:image/[png|jpeg|bmp|webp];base64,[base64 data], where **[base64 data]** indicates a Base64 string. 138e41f4b71Sopenharmony_ci 139e41f4b71Sopenharmony_ci Base64 strings are widely used on web pages for storing pixel data of images. 140e41f4b71Sopenharmony_ci 141e41f4b71Sopenharmony_ci 142e41f4b71Sopenharmony_ci### Pixel Map 143e41f4b71Sopenharmony_ci 144e41f4b71Sopenharmony_ciA pixel map is a pixel image obtained after image decoding. For details, see [Introduction to Image Kit](../media/image/image-overview.md). In the following example, the data returned by the loaded online image is decoded into a pixel map, which is then displayed on the **Image** component. 145e41f4b71Sopenharmony_ci 146e41f4b71Sopenharmony_ci1. Create a **PixelMap** state variable. 147e41f4b71Sopenharmony_ci 148e41f4b71Sopenharmony_ci ```ts 149e41f4b71Sopenharmony_ci @State image: PixelMap | undefined = undefined; 150e41f4b71Sopenharmony_ci ``` 151e41f4b71Sopenharmony_ci 152e41f4b71Sopenharmony_ci2. Reference multimedia. 153e41f4b71Sopenharmony_ci 154e41f4b71Sopenharmony_ci Request an online image and implement transcoding to generate a pixel map. 155e41f4b71Sopenharmony_ci 156e41f4b71Sopenharmony_ci 1. Reference the network and media library access permissions. 157e41f4b71Sopenharmony_ci ```ts 158e41f4b71Sopenharmony_ci import { http } from '@kit.NetworkKit'; 159e41f4b71Sopenharmony_ci import { image } from '@kit.ImageKit'; 160e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 161e41f4b71Sopenharmony_ci ``` 162e41f4b71Sopenharmony_ci 2. Enter the online image address. 163e41f4b71Sopenharmony_ci ```ts 164e41f4b71Sopenharmony_ci let OutData: http.HttpResponse 165e41f4b71Sopenharmony_ci http.createHttp().request("https://www.example.com/xxx.png", 166e41f4b71Sopenharmony_ci (error: BusinessError, data: http.HttpResponse) => { 167e41f4b71Sopenharmony_ci if (error) { 168e41f4b71Sopenharmony_ci console.error(`http request failed with. Code: ${error.code}, message: ${error.message}`); 169e41f4b71Sopenharmony_ci } else { 170e41f4b71Sopenharmony_ci OutData = data 171e41f4b71Sopenharmony_ci } 172e41f4b71Sopenharmony_ci } 173e41f4b71Sopenharmony_ci ) 174e41f4b71Sopenharmony_ci ``` 175e41f4b71Sopenharmony_ci 3. Transcode the data returned by the online image address to a pixel map. 176e41f4b71Sopenharmony_ci ```ts 177e41f4b71Sopenharmony_ci let code: http.ResponseCode | number = OutData.responseCode 178e41f4b71Sopenharmony_ci if (http.ResponseCode.OK === code) { 179e41f4b71Sopenharmony_ci let imageData: ArrayBuffer = OutData.result as ArrayBuffer; 180e41f4b71Sopenharmony_ci let imageSource: image.ImageSource = image.createImageSource(imageData); 181e41f4b71Sopenharmony_ci 182e41f4b71Sopenharmony_ci class tmp { 183e41f4b71Sopenharmony_ci height: number = 100 184e41f4b71Sopenharmony_ci width: number = 100 185e41f4b71Sopenharmony_ci } 186e41f4b71Sopenharmony_ci 187e41f4b71Sopenharmony_ci let si: tmp = new tmp() 188e41f4b71Sopenharmony_ci let options: Record<string, number | boolean | tmp> = { 189e41f4b71Sopenharmony_ci 'alphaType': 0, // Alpha type. 190e41f4b71Sopenharmony_ci 'editable': false, // Whether the image is editable. 191e41f4b71Sopenharmony_ci 'pixelFormat': 3, // Pixel format. 192e41f4b71Sopenharmony_ci 'scaleMode': 1, // Scale mode. 193e41f4b71Sopenharmony_ci 'size': { height: 100, width: 100 } 194e41f4b71Sopenharmony_ci } // Image size. 195e41f4b71Sopenharmony_ci 196e41f4b71Sopenharmony_ci class imagetmp { 197e41f4b71Sopenharmony_ci image: PixelMap | undefined = undefined 198e41f4b71Sopenharmony_ci set(val: PixelMap) { 199e41f4b71Sopenharmony_ci this.image = val 200e41f4b71Sopenharmony_ci } 201e41f4b71Sopenharmony_ci } 202e41f4b71Sopenharmony_ci 203e41f4b71Sopenharmony_ci imageSource.createPixelMap(options).then((pixelMap: PixelMap) => { 204e41f4b71Sopenharmony_ci let im = new imagetmp() 205e41f4b71Sopenharmony_ci im.set(pixelMap) 206e41f4b71Sopenharmony_ci }) 207e41f4b71Sopenharmony_ci } 208e41f4b71Sopenharmony_ci ``` 209e41f4b71Sopenharmony_ci 4. Display the image. 210e41f4b71Sopenharmony_ci ```ts 211e41f4b71Sopenharmony_ci class htp{ 212e41f4b71Sopenharmony_ci httpRequest: Function | undefined = undefined 213e41f4b71Sopenharmony_ci set(){ 214e41f4b71Sopenharmony_ci if(this.httpRequest){ 215e41f4b71Sopenharmony_ci this.httpRequest() 216e41f4b71Sopenharmony_ci } 217e41f4b71Sopenharmony_ci } 218e41f4b71Sopenharmony_ci } 219e41f4b71Sopenharmony_ci Button ("Get Online Image") 220e41f4b71Sopenharmony_ci .onClick(() => { 221e41f4b71Sopenharmony_ci let sethtp = new htp() 222e41f4b71Sopenharmony_ci sethtp.set() 223e41f4b71Sopenharmony_ci }) 224e41f4b71Sopenharmony_ci Image(this.image).height(100).width(100) 225e41f4b71Sopenharmony_ci ``` 226e41f4b71Sopenharmony_ci You can also pass **pixelMap** to create a [PixelMapDrawableDescriptor](../reference/apis-arkui/js-apis-arkui-drawableDescriptor.md#pixelmapdrawabledescriptor12) object for displaying images. 227e41f4b71Sopenharmony_ci ```ts 228e41f4b71Sopenharmony_ci import { DrawableDescriptor, PixelMapDrawableDescriptor } from '@kit.ArkUI' 229e41f4b71Sopenharmony_ci class htp{ 230e41f4b71Sopenharmony_ci httpRequest: Function | undefined = undefined 231e41f4b71Sopenharmony_ci set(){ 232e41f4b71Sopenharmony_ci if(this.httpRequest){ 233e41f4b71Sopenharmony_ci this.httpRequest() 234e41f4b71Sopenharmony_ci } 235e41f4b71Sopenharmony_ci } 236e41f4b71Sopenharmony_ci } 237e41f4b71Sopenharmony_ci Button ("Get Online Image") 238e41f4b71Sopenharmony_ci .onClick(() => { 239e41f4b71Sopenharmony_ci let sethtp = new htp() 240e41f4b71Sopenharmony_ci sethtp.set() 241e41f4b71Sopenharmony_ci this.drawablePixelMap = new PixelMapDrawableDescriptor(this.image) 242e41f4b71Sopenharmony_ci }) 243e41f4b71Sopenharmony_ci Image(this.drawablePixelMap).height(100).width(100) 244e41f4b71Sopenharmony_ci ``` 245e41f4b71Sopenharmony_ci 246e41f4b71Sopenharmony_ci 247e41f4b71Sopenharmony_ci## Displaying Vector Images 248e41f4b71Sopenharmony_ci 249e41f4b71Sopenharmony_ciThe **Image** component can display vector images in SVG format. The supported SVG labels are **svg**, **rect**, **circle**, **ellipse**, **path**, **line**, **polyline**, **polygon**, and **animate**. 250e41f4b71Sopenharmony_ci 251e41f4b71Sopenharmony_ciYou can use the **fillColor** attribute to change the fill color of an SVG image. 252e41f4b71Sopenharmony_ci 253e41f4b71Sopenharmony_ci 254e41f4b71Sopenharmony_ci```ts 255e41f4b71Sopenharmony_ciImage($r('app.media.cloud')) 256e41f4b71Sopenharmony_ci .width(50) 257e41f4b71Sopenharmony_ci .fillColor(Color.Blue) 258e41f4b71Sopenharmony_ci``` 259e41f4b71Sopenharmony_ci 260e41f4b71Sopenharmony_ci **Figure 3** Original image 261e41f4b71Sopenharmony_ci 262e41f4b71Sopenharmony_ci 263e41f4b71Sopenharmony_ci 264e41f4b71Sopenharmony_ci **Figure 4** SVG image after the fill color is set 265e41f4b71Sopenharmony_ci 266e41f4b71Sopenharmony_ci 267e41f4b71Sopenharmony_ci 268e41f4b71Sopenharmony_ci 269e41f4b71Sopenharmony_ci## Setting Attributes 270e41f4b71Sopenharmony_ci 271e41f4b71Sopenharmony_ciSetting attributes for the **Image** component can spruce up the image with custom effects. The following are usage examples of common attributes. For details about all attributes, see [Image](../reference/apis-arkui/arkui-ts/ts-basic-components-image.md). 272e41f4b71Sopenharmony_ci 273e41f4b71Sopenharmony_ci 274e41f4b71Sopenharmony_ci### Setting the Image Scale Mode 275e41f4b71Sopenharmony_ci 276e41f4b71Sopenharmony_ciYou can use the **objectFit** attribute to scale an image to fit it into a container whose height and width are determined. 277e41f4b71Sopenharmony_ci 278e41f4b71Sopenharmony_ci 279e41f4b71Sopenharmony_ci```ts 280e41f4b71Sopenharmony_ci@Entry 281e41f4b71Sopenharmony_ci@Component 282e41f4b71Sopenharmony_cistruct MyComponent { 283e41f4b71Sopenharmony_ci scroller: Scroller = new Scroller() 284e41f4b71Sopenharmony_ci 285e41f4b71Sopenharmony_ci build() { 286e41f4b71Sopenharmony_ci Scroll(this.scroller) { 287e41f4b71Sopenharmony_ci Column() { 288e41f4b71Sopenharmony_ci Row() { 289e41f4b71Sopenharmony_ci Image($r('app.media.img_2')) 290e41f4b71Sopenharmony_ci .width(200) 291e41f4b71Sopenharmony_ci .height(150) 292e41f4b71Sopenharmony_ci .border({ width: 1 }) 293e41f4b71Sopenharmony_ci // The image is scaled with its aspect ratio retained for the content to be completely displayed within the display boundaries. 294e41f4b71Sopenharmony_ci .objectFit(ImageFit.Contain) 295e41f4b71Sopenharmony_ci .margin(15) 296e41f4b71Sopenharmony_ci .overlay('Contain', { align: Alignment.Bottom, offset: { x: 0, y: 20 } }) 297e41f4b71Sopenharmony_ci Image($r('app.media.ic_img_2')) 298e41f4b71Sopenharmony_ci .width(200) 299e41f4b71Sopenharmony_ci .height(150) 300e41f4b71Sopenharmony_ci .border({ width: 1 }) 301e41f4b71Sopenharmony_ci .objectFit(ImageFit.Cover) 302e41f4b71Sopenharmony_ci .margin(15) 303e41f4b71Sopenharmony_ci // The image is scaled with its aspect ratio retained for both sides to be greater than or equal to the display boundaries. 304e41f4b71Sopenharmony_ci .overlay('Cover', { align: Alignment.Bottom, offset: { x: 0, y: 20 } }) 305e41f4b71Sopenharmony_ci Image($r('app.media.img_2')) 306e41f4b71Sopenharmony_ci .width(200) 307e41f4b71Sopenharmony_ci .height(150) 308e41f4b71Sopenharmony_ci .border({ width: 1 }) 309e41f4b71Sopenharmony_ci // The image is scaled automatically to fit the display area. 310e41f4b71Sopenharmony_ci .objectFit(ImageFit.Auto) 311e41f4b71Sopenharmony_ci .margin(15) 312e41f4b71Sopenharmony_ci .overlay('Auto', { align: Alignment.Bottom, offset: { x: 0, y: 20 } }) 313e41f4b71Sopenharmony_ci } 314e41f4b71Sopenharmony_ci 315e41f4b71Sopenharmony_ci Row() { 316e41f4b71Sopenharmony_ci Image($r('app.media.img_2')) 317e41f4b71Sopenharmony_ci .width(200) 318e41f4b71Sopenharmony_ci .height(150) 319e41f4b71Sopenharmony_ci .border({ width: 1 }) 320e41f4b71Sopenharmony_ci // The image is scaled to fill the display area, and its aspect ratio is not retained. 321e41f4b71Sopenharmony_ci .objectFit(ImageFit.Fill) 322e41f4b71Sopenharmony_ci .margin(15) 323e41f4b71Sopenharmony_ci .overlay('Fill', { align: Alignment.Bottom, offset: { x: 0, y: 20 } }) 324e41f4b71Sopenharmony_ci Image($r('app.media.img_2')) 325e41f4b71Sopenharmony_ci .width(200) 326e41f4b71Sopenharmony_ci .height(150) 327e41f4b71Sopenharmony_ci .border({ width: 1 }) 328e41f4b71Sopenharmony_ci // The image content is displayed with its aspect ratio retained. The size is smaller than or equal to the original size. 329e41f4b71Sopenharmony_ci .objectFit(ImageFit.ScaleDown) 330e41f4b71Sopenharmony_ci .margin(15) 331e41f4b71Sopenharmony_ci .overlay('ScaleDown', { align: Alignment.Bottom, offset: { x: 0, y: 20 } }) 332e41f4b71Sopenharmony_ci Image($r('app.media.img_2')) 333e41f4b71Sopenharmony_ci .width(200) 334e41f4b71Sopenharmony_ci .height(150) 335e41f4b71Sopenharmony_ci .border({ width: 1 }) 336e41f4b71Sopenharmony_ci // The original size is retained. 337e41f4b71Sopenharmony_ci .objectFit(ImageFit.None) 338e41f4b71Sopenharmony_ci .margin(15) 339e41f4b71Sopenharmony_ci .overlay('None', { align: Alignment.Bottom, offset: { x: 0, y: 20 } }) 340e41f4b71Sopenharmony_ci } 341e41f4b71Sopenharmony_ci } 342e41f4b71Sopenharmony_ci } 343e41f4b71Sopenharmony_ci } 344e41f4b71Sopenharmony_ci} 345e41f4b71Sopenharmony_ci``` 346e41f4b71Sopenharmony_ci 347e41f4b71Sopenharmony_ci 348e41f4b71Sopenharmony_ci 349e41f4b71Sopenharmony_ci 350e41f4b71Sopenharmony_ci### Using Image Interpolation 351e41f4b71Sopenharmony_ci 352e41f4b71Sopenharmony_ciAn image of low resolution may suffer quality loss with aliasing when scaled up. If this is the case, you can use the **interpolation** attribute to conduct image interpolation and improve image quality. 353e41f4b71Sopenharmony_ci 354e41f4b71Sopenharmony_ci 355e41f4b71Sopenharmony_ci```ts 356e41f4b71Sopenharmony_ci@Entry 357e41f4b71Sopenharmony_ci@Component 358e41f4b71Sopenharmony_cistruct Index { 359e41f4b71Sopenharmony_ci build() { 360e41f4b71Sopenharmony_ci Column() { 361e41f4b71Sopenharmony_ci Row() { 362e41f4b71Sopenharmony_ci Image($r('app.media.grass')) 363e41f4b71Sopenharmony_ci .width('40%') 364e41f4b71Sopenharmony_ci .interpolation(ImageInterpolation.None) 365e41f4b71Sopenharmony_ci .borderWidth(1) 366e41f4b71Sopenharmony_ci .overlay("Interpolation.None", { align: Alignment.Bottom, offset: { x: 0, y: 20 } }) 367e41f4b71Sopenharmony_ci .margin(10) 368e41f4b71Sopenharmony_ci Image($r('app.media.grass')) 369e41f4b71Sopenharmony_ci .width('40%') 370e41f4b71Sopenharmony_ci .interpolation(ImageInterpolation.Low) 371e41f4b71Sopenharmony_ci .borderWidth(1) 372e41f4b71Sopenharmony_ci .overlay("Interpolation.Low", { align: Alignment.Bottom, offset: { x: 0, y: 20 } }) 373e41f4b71Sopenharmony_ci .margin(10) 374e41f4b71Sopenharmony_ci }.width('100%') 375e41f4b71Sopenharmony_ci .justifyContent(FlexAlign.Center) 376e41f4b71Sopenharmony_ci 377e41f4b71Sopenharmony_ci Row() { 378e41f4b71Sopenharmony_ci Image($r('app.media.grass')) 379e41f4b71Sopenharmony_ci .width('40%') 380e41f4b71Sopenharmony_ci .interpolation(ImageInterpolation.Medium) 381e41f4b71Sopenharmony_ci .borderWidth(1) 382e41f4b71Sopenharmony_ci .overlay("Interpolation.Medium", { align: Alignment.Bottom, offset: { x: 0, y: 20 } }) 383e41f4b71Sopenharmony_ci .margin(10) 384e41f4b71Sopenharmony_ci Image($r('app.media.grass')) 385e41f4b71Sopenharmony_ci .width('40%') 386e41f4b71Sopenharmony_ci .interpolation(ImageInterpolation.High) 387e41f4b71Sopenharmony_ci .borderWidth(1) 388e41f4b71Sopenharmony_ci .overlay("Interpolation.High", { align: Alignment.Bottom, offset: { x: 0, y: 20 } }) 389e41f4b71Sopenharmony_ci .margin(10) 390e41f4b71Sopenharmony_ci }.width('100%') 391e41f4b71Sopenharmony_ci .justifyContent(FlexAlign.Center) 392e41f4b71Sopenharmony_ci } 393e41f4b71Sopenharmony_ci .height('100%') 394e41f4b71Sopenharmony_ci } 395e41f4b71Sopenharmony_ci} 396e41f4b71Sopenharmony_ci``` 397e41f4b71Sopenharmony_ci 398e41f4b71Sopenharmony_ci 399e41f4b71Sopenharmony_ci 400e41f4b71Sopenharmony_ci 401e41f4b71Sopenharmony_ci### Setting Image Repeat Pattern 402e41f4b71Sopenharmony_ci 403e41f4b71Sopenharmony_ciYou can use the **objectRepeat** attribute to set the repeat pattern of an image. For details, see [ImageRepeat](../reference/apis-arkui/arkui-ts/ts-appendix-enums.md#imagerepeat). 404e41f4b71Sopenharmony_ci 405e41f4b71Sopenharmony_ci 406e41f4b71Sopenharmony_ci```ts 407e41f4b71Sopenharmony_ci@Entry 408e41f4b71Sopenharmony_ci@Component 409e41f4b71Sopenharmony_cistruct MyComponent { 410e41f4b71Sopenharmony_ci build() { 411e41f4b71Sopenharmony_ci Column({ space: 10 }) { 412e41f4b71Sopenharmony_ci Row({ space: 5 }) { 413e41f4b71Sopenharmony_ci Image($r('app.media.ic_public_favor_filled_1')) 414e41f4b71Sopenharmony_ci .width(110) 415e41f4b71Sopenharmony_ci .height(115) 416e41f4b71Sopenharmony_ci .border({ width: 1 }) 417e41f4b71Sopenharmony_ci .objectRepeat(ImageRepeat.XY) 418e41f4b71Sopenharmony_ci .objectFit(ImageFit.ScaleDown) 419e41f4b71Sopenharmony_ci // Repeat the image along both the horizontal and vertical axes. 420e41f4b71Sopenharmony_ci .overlay('ImageRepeat.XY', { align: Alignment.Bottom, offset: { x: 0, y: 20 } }) 421e41f4b71Sopenharmony_ci Image($r('app.media.ic_public_favor_filled_1')) 422e41f4b71Sopenharmony_ci .width(110) 423e41f4b71Sopenharmony_ci .height(115) 424e41f4b71Sopenharmony_ci .border({ width: 1 }) 425e41f4b71Sopenharmony_ci .objectRepeat(ImageRepeat.Y) 426e41f4b71Sopenharmony_ci .objectFit(ImageFit.ScaleDown) 427e41f4b71Sopenharmony_ci // Repeat the image only along the vertical axis. 428e41f4b71Sopenharmony_ci .overlay('ImageRepeat.Y', { align: Alignment.Bottom, offset: { x: 0, y: 20 } }) 429e41f4b71Sopenharmony_ci Image($r('app.media.ic_public_favor_filled_1')) 430e41f4b71Sopenharmony_ci .width(110) 431e41f4b71Sopenharmony_ci .height(115) 432e41f4b71Sopenharmony_ci .border({ width: 1 }) 433e41f4b71Sopenharmony_ci .objectRepeat(ImageRepeat.X) 434e41f4b71Sopenharmony_ci .objectFit(ImageFit.ScaleDown) 435e41f4b71Sopenharmony_ci // Repeat the image only along the horizontal axis. 436e41f4b71Sopenharmony_ci .overlay('ImageRepeat.X', { align: Alignment.Bottom, offset: { x: 0, y: 20 } }) 437e41f4b71Sopenharmony_ci } 438e41f4b71Sopenharmony_ci }.height(150).width('100%').padding(8) 439e41f4b71Sopenharmony_ci } 440e41f4b71Sopenharmony_ci} 441e41f4b71Sopenharmony_ci``` 442e41f4b71Sopenharmony_ci 443e41f4b71Sopenharmony_ci 444e41f4b71Sopenharmony_ci 445e41f4b71Sopenharmony_ci 446e41f4b71Sopenharmony_ci### Setting Image Rendering Mode 447e41f4b71Sopenharmony_ci 448e41f4b71Sopenharmony_ciYou can use the **renderMode** attribute to set the rendering mode of an image. 449e41f4b71Sopenharmony_ci 450e41f4b71Sopenharmony_ci 451e41f4b71Sopenharmony_ci```ts 452e41f4b71Sopenharmony_ci@Entry 453e41f4b71Sopenharmony_ci@Component 454e41f4b71Sopenharmony_cistruct MyComponent { 455e41f4b71Sopenharmony_ci build() { 456e41f4b71Sopenharmony_ci Column({ space: 10 }) { 457e41f4b71Sopenharmony_ci Row({ space: 50 }) { 458e41f4b71Sopenharmony_ci Image($r('app.media.example')) 459e41f4b71Sopenharmony_ci // Set the rendering mode to Original. 460e41f4b71Sopenharmony_ci .renderMode(ImageRenderMode.Original) 461e41f4b71Sopenharmony_ci .width(100) 462e41f4b71Sopenharmony_ci .height(100) 463e41f4b71Sopenharmony_ci .border({ width: 1 }) 464e41f4b71Sopenharmony_ci // overlay is a universal attribute, which is used to add overlay text on the component. 465e41f4b71Sopenharmony_ci .overlay('Original', { align: Alignment.Bottom, offset: { x: 0, y: 20 } }) 466e41f4b71Sopenharmony_ci Image($r('app.media.example')) 467e41f4b71Sopenharmony_ci // Set the rendering mode to Template. 468e41f4b71Sopenharmony_ci .renderMode(ImageRenderMode.Template) 469e41f4b71Sopenharmony_ci .width(100) 470e41f4b71Sopenharmony_ci .height(100) 471e41f4b71Sopenharmony_ci .border({ width: 1 }) 472e41f4b71Sopenharmony_ci .overlay('Template', { align: Alignment.Bottom, offset: { x: 0, y: 20 } }) 473e41f4b71Sopenharmony_ci } 474e41f4b71Sopenharmony_ci }.height(150).width('100%').padding({ top: 20,right: 10 }) 475e41f4b71Sopenharmony_ci } 476e41f4b71Sopenharmony_ci} 477e41f4b71Sopenharmony_ci``` 478e41f4b71Sopenharmony_ci 479e41f4b71Sopenharmony_ci 480e41f4b71Sopenharmony_ci 481e41f4b71Sopenharmony_ci 482e41f4b71Sopenharmony_ci### Setting Image Decoding Size 483e41f4b71Sopenharmony_ci 484e41f4b71Sopenharmony_ciYou can use the **sourceSize** attribute to set the image decoding size. By setting the decoding size to lower than the source size, you can decrease the image resolution. 485e41f4b71Sopenharmony_ci 486e41f4b71Sopenharmony_ciIn this example, the source image size is 1280 x 960, and the decoding sizes are 40 x 40 and 90 x 90. 487e41f4b71Sopenharmony_ci 488e41f4b71Sopenharmony_ci 489e41f4b71Sopenharmony_ci```ts 490e41f4b71Sopenharmony_ci@Entry 491e41f4b71Sopenharmony_ci@Component 492e41f4b71Sopenharmony_cistruct Index { 493e41f4b71Sopenharmony_ci build() { 494e41f4b71Sopenharmony_ci Column() { 495e41f4b71Sopenharmony_ci Row({ space: 50 }) { 496e41f4b71Sopenharmony_ci Image($r('app.media.example')) 497e41f4b71Sopenharmony_ci .sourceSize({ 498e41f4b71Sopenharmony_ci width: 40, 499e41f4b71Sopenharmony_ci height: 40 500e41f4b71Sopenharmony_ci }) 501e41f4b71Sopenharmony_ci .objectFit(ImageFit.ScaleDown) 502e41f4b71Sopenharmony_ci .aspectRatio(1) 503e41f4b71Sopenharmony_ci .width('25%') 504e41f4b71Sopenharmony_ci .border({ width: 1 }) 505e41f4b71Sopenharmony_ci .overlay('width:40 height:40', { align: Alignment.Bottom, offset: { x: 0, y: 40 } }) 506e41f4b71Sopenharmony_ci Image($r('app.media.example')) 507e41f4b71Sopenharmony_ci .sourceSize({ 508e41f4b71Sopenharmony_ci width: 90, 509e41f4b71Sopenharmony_ci height: 90 510e41f4b71Sopenharmony_ci }) 511e41f4b71Sopenharmony_ci .objectFit(ImageFit.ScaleDown) 512e41f4b71Sopenharmony_ci .width('25%') 513e41f4b71Sopenharmony_ci .aspectRatio(1) 514e41f4b71Sopenharmony_ci .border({ width: 1 }) 515e41f4b71Sopenharmony_ci .overlay('width:90 height:90', { align: Alignment.Bottom, offset: { x: 0, y: 40 } }) 516e41f4b71Sopenharmony_ci }.height(150).width('100%').padding(20) 517e41f4b71Sopenharmony_ci } 518e41f4b71Sopenharmony_ci } 519e41f4b71Sopenharmony_ci} 520e41f4b71Sopenharmony_ci``` 521e41f4b71Sopenharmony_ci 522e41f4b71Sopenharmony_ci 523e41f4b71Sopenharmony_ci 524e41f4b71Sopenharmony_ci 525e41f4b71Sopenharmony_ci### Adding a Filter to an Image 526e41f4b71Sopenharmony_ci 527e41f4b71Sopenharmony_ciYou can use the **colorFilter** attribute to add a filter to an image. 528e41f4b71Sopenharmony_ci 529e41f4b71Sopenharmony_ci 530e41f4b71Sopenharmony_ci```ts 531e41f4b71Sopenharmony_ci@Entry 532e41f4b71Sopenharmony_ci@Component 533e41f4b71Sopenharmony_cistruct Index { 534e41f4b71Sopenharmony_ci build() { 535e41f4b71Sopenharmony_ci Column() { 536e41f4b71Sopenharmony_ci Row() { 537e41f4b71Sopenharmony_ci Image($r('app.media.example')) 538e41f4b71Sopenharmony_ci .width('40%') 539e41f4b71Sopenharmony_ci .margin(10) 540e41f4b71Sopenharmony_ci Image($r('app.media.example')) 541e41f4b71Sopenharmony_ci .width('40%') 542e41f4b71Sopenharmony_ci .colorFilter( 543e41f4b71Sopenharmony_ci [1, 1, 0, 0, 0, 544e41f4b71Sopenharmony_ci 0, 1, 0, 0, 0, 545e41f4b71Sopenharmony_ci 0, 0, 1, 0, 0, 546e41f4b71Sopenharmony_ci 0, 0, 0, 1, 0]) 547e41f4b71Sopenharmony_ci .margin(10) 548e41f4b71Sopenharmony_ci }.width('100%') 549e41f4b71Sopenharmony_ci .justifyContent(FlexAlign.Center) 550e41f4b71Sopenharmony_ci } 551e41f4b71Sopenharmony_ci } 552e41f4b71Sopenharmony_ci} 553e41f4b71Sopenharmony_ci``` 554e41f4b71Sopenharmony_ci 555e41f4b71Sopenharmony_ci 556e41f4b71Sopenharmony_ci 557e41f4b71Sopenharmony_ci 558e41f4b71Sopenharmony_ci### Synchronously Loading Images 559e41f4b71Sopenharmony_ci 560e41f4b71Sopenharmony_ciGenerally, the image loading process is performed asynchronously to avoid blocking the main thread and to streamline UI interaction. In certain cases, however, the image may flicker when refreshed. If this occurs, you can use the **syncLoad** attribute to load the image synchronously to avoid flickering. Avoid using this attribute if the image loading may take a long time. Otherwise, the page may fail to respond. 561e41f4b71Sopenharmony_ci 562e41f4b71Sopenharmony_ci 563e41f4b71Sopenharmony_ci```ts 564e41f4b71Sopenharmony_ciImage($r('app.media.icon')) 565e41f4b71Sopenharmony_ci .syncLoad(true) 566e41f4b71Sopenharmony_ci``` 567e41f4b71Sopenharmony_ci 568e41f4b71Sopenharmony_ci 569e41f4b71Sopenharmony_ci## Adding Events 570e41f4b71Sopenharmony_ci 571e41f4b71Sopenharmony_ciBy binding the **onComplete** event to the **Image** component, you can obtain necessary information about the image after the image is successfully loaded. You can also bind the **onError** event to obtain error information when the image fails to be loaded. 572e41f4b71Sopenharmony_ci 573e41f4b71Sopenharmony_ci 574e41f4b71Sopenharmony_ci```ts 575e41f4b71Sopenharmony_ci@Entry 576e41f4b71Sopenharmony_ci@Component 577e41f4b71Sopenharmony_cistruct MyComponent { 578e41f4b71Sopenharmony_ci @State widthValue: number = 0 579e41f4b71Sopenharmony_ci @State heightValue: number = 0 580e41f4b71Sopenharmony_ci @State componentWidth: number = 0 581e41f4b71Sopenharmony_ci @State componentHeight: number = 0 582e41f4b71Sopenharmony_ci 583e41f4b71Sopenharmony_ci build() { 584e41f4b71Sopenharmony_ci Column() { 585e41f4b71Sopenharmony_ci Row() { 586e41f4b71Sopenharmony_ci Image($r('app.media.ic_img_2')) 587e41f4b71Sopenharmony_ci .width(200) 588e41f4b71Sopenharmony_ci .height(150) 589e41f4b71Sopenharmony_ci .margin(15) 590e41f4b71Sopenharmony_ci .onComplete(msg => { 591e41f4b71Sopenharmony_ci if(msg){ 592e41f4b71Sopenharmony_ci this.widthValue = msg.width 593e41f4b71Sopenharmony_ci this.heightValue = msg.height 594e41f4b71Sopenharmony_ci this.componentWidth = msg.componentWidth 595e41f4b71Sopenharmony_ci this.componentHeight = msg.componentHeight 596e41f4b71Sopenharmony_ci } 597e41f4b71Sopenharmony_ci }) 598e41f4b71Sopenharmony_ci // If the image fails to be obtained, print the result. 599e41f4b71Sopenharmony_ci .onError(() => { 600e41f4b71Sopenharmony_ci console.info('load image fail') 601e41f4b71Sopenharmony_ci }) 602e41f4b71Sopenharmony_ci .overlay('\nwidth: ' + String(this.widthValue) + ', height: ' + String(this.heightValue) + '\ncomponentWidth: ' + String(this.componentWidth) + '\ncomponentHeight: ' + String(this.componentHeight), { 603e41f4b71Sopenharmony_ci align: Alignment.Bottom, 604e41f4b71Sopenharmony_ci offset: { x: 0, y: 60 } 605e41f4b71Sopenharmony_ci }) 606e41f4b71Sopenharmony_ci } 607e41f4b71Sopenharmony_ci } 608e41f4b71Sopenharmony_ci } 609e41f4b71Sopenharmony_ci} 610e41f4b71Sopenharmony_ci``` 611e41f4b71Sopenharmony_ci 612e41f4b71Sopenharmony_ci 613e41f4b71Sopenharmony_ci 614