1# @ohos.arkui.componentSnapshot (Component Snapshot) 2 3The **componentSnapshot** module provides APIs for obtaining component snapshots, including snapshots of components that have been loaded and snapshots of components that have not been loaded yet. Note that a component snapshot does not contain content drawn outside of the area of the owning component or the parent component. 4 5> **NOTE** 6> 7> The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8> 9> You can preview how this component looks on a real device. The preview is not yet available in the DevEco Studio Previewer. 10 11 12## Modules to Import 13 14```ts 15import componentSnapshot from "@ohos.arkui.componentSnapshot"; 16``` 17 18## componentSnapshot.get 19 20get(id: string, callback: AsyncCallback<image.PixelMap>): void 21 22Obtains the snapshot of a component that has been loaded. This API uses an asynchronous callback to return the result. 23 24> **NOTE** 25> 26> The snapshot captures content rendered in the last frame. If this API is called when the component triggers an update, the re-rendered content will not be included in the obtained snapshot. 27 28**System capability**: SystemCapability.ArkUI.ArkUI.Full 29 30**Parameters** 31 32| Name | Type | Mandatory | Description | 33| -------- | ----------------------------------- | ---- | ---------------------------------------- | 34| id | string | Yes | [ID](../arkui-ts/ts-universal-attributes-component-id.md) of the target component.| 35| callback | AsyncCallback<image.PixelMap> | Yes | Callback used to return the result. | 36 37**Error codes** 38 39| ID| Error Message | 40| -------- | ------------------- | 41| 100001 | if id is not valid. | 42 43**Example** 44 45```ts 46import componentSnapshot from '@ohos.arkui.componentSnapshot' 47import image from '@ohos.multimedia.image' 48 49@Entry 50@Component 51struct SnapshotExample { 52 @State pixmap: image.PixelMap|undefined = undefined 53 54 build() { 55 Column() { 56 Image(this.pixmap) 57 .width(300).height(300) 58 // ...Component 59 // ...Component 60 // ...Component 61 Button("click to generate UI snapshot") 62 .onClick(() => { 63 componentSnapshot.get("root", (error: Error, pixmap: image.PixelMap) => { 64 this.pixmap = pixmap 65 // save pixmap to file 66 // .... 67 }) 68 }) 69 } 70 .width('80%') 71 .margin({ left: 10, top: 5, bottom: 5 }) 72 .height(200) 73 .border({ color: '#880606', width: 2 }) 74 .id("root") 75 } 76} 77``` 78 79## componentSnapshot.get 80 81get(id: string): Promise<image.PixelMap> 82 83Obtains the snapshot of a component that has been loaded. This API uses a promise to return the result. 84 85> **NOTE** 86> 87> The snapshot captures content rendered in the last frame. If this API is called when the component triggers an update, the re-rendered content will not be included in the obtained snapshot. 88 89**System capability**: SystemCapability.ArkUI.ArkUI.Full 90 91**Parameters** 92 93| Name | Type | Mandatory | Description | 94| ---- | ------ | ---- | ---------------------------------------- | 95| id | string | Yes | [ID](../arkui-ts/ts-universal-attributes-component-id.md) of the target component.| 96 97**Return value** 98 99| Type | Description | 100| ----------------------------- | -------- | 101| Promise<image.PixelMap> | Promise used to return the result.| 102 103**Error codes** 104 105| ID | Error Message | 106| ------ | ------------------- | 107| 100001 | if id is not valid. | 108 109**Example** 110 111```ts 112import componentSnapshot from '@ohos.arkui.componentSnapshot' 113import image from '@ohos.multimedia.image' 114 115@Entry 116@Component 117struct SnapshotExample { 118 @State pixmap: image.PixelMap|undefined = undefined 119 120 build() { 121 Column() { 122 Image(this.pixmap) 123 .width(300).height(300) 124 // ...Component 125 // ...Component 126 // ...Component 127 Button("click to generate UI snapshot") 128 .onClick(() => { 129 componentSnapshot.get("root") 130 .then((pixmap: image.PixelMap) => { 131 this.pixmap = pixmap 132 // save pixmap to file 133 // .... 134 }) 135 }) 136 } 137 .width('80%') 138 .margin({ left: 10, top: 5, bottom: 5 }) 139 .height(200) 140 .border({ color: '#880606', width: 2 }) 141 .id("root") 142 } 143} 144``` 145 146## componentSnapshot.createFromBuilder 147 148createFromBuilder(builder: CustomBuilder, callback: AsyncCallback<image.PixelMap>): void 149 150Renders a custom component in the application background and outputs its snapshot. This API uses an asynchronous callback to return the result. 151 152> **NOTE** 153> 154> To account for the time spent in awaiting component building and rendering, the callback of offscreen snapshots has a delay of less than 500 ms. 155> 156> If a component is on a time-consuming task, for example, an **\<Image>** or **\<Web>** component that is loading online images, its loading may be still in progress when this API is called. In this case, the output snapshot does not represent the component in the way it looks when the loading is successfully completed. 157 158 159**System capability**: SystemCapability.ArkUI.ArkUI.Full 160 161**Parameters** 162 163| Name | Type | Mandatory | Description | 164| -------- | ---------------------------------------- | ---- | ---------- | 165| builder | [CustomBuilder](../arkui-ts/ts-types.md#custombuilder8) | Yes | Builder of the custom component.| 166| callback | AsyncCallback<image.PixelMap> | Yes | Callback used to return the result.| 167 168**Error codes** 169 170| ID| Error Message | 171| -------- | ----------------------------------------- | 172| 100001 | if builder is not a valid build function. | 173 174**Example** 175 176```ts 177import componentSnapshot from '@ohos.arkui.componentSnapshot' 178import image from '@ohos.multimedia.image' 179 180@Entry 181@Component 182struct OffscreenSnapshotExample { 183 @State pixmap: image.PixelMap | undefined = undefined 184 185 @Builder 186 RandomBuilder() { 187 Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) { 188 Text('Test menu item 1') 189 .fontSize(20) 190 .width(100) 191 .height(50) 192 .textAlign(TextAlign.Center) 193 Divider().height(10) 194 Text('Test menu item 2') 195 .fontSize(20) 196 .width(100) 197 .height(50) 198 .textAlign(TextAlign.Center) 199 }.width(100) 200 } 201 202 build() { 203 Column() { 204 Button("click to generate offscreen UI snapshot") 205 .onClick(() => { 206 componentSnapshot.createFromBuilder(this.RandomBuilder(), 207 (error: Error, pixmap: image.PixelMap) => { 208 this.pixmap = pixmap 209 // save pixmap to file 210 // .... 211 }) 212 }) 213 }.width('80%').margin({ left: 10, top: 5, bottom: 5 }).height(200) 214 .border({ color: '#880606', width: 2 }) 215 } 216} 217``` 218 219## componentSnapshot.createFromBuilder 220 221createFromBuilder(builder: CustomBuilder): Promise<image.PixelMap> 222 223Renders a custom component in the application background and outputs its snapshot. This API uses a promise to return the result. 224 225> **NOTE** 226> 227> To account for the time spent in awaiting component building and rendering, the callback of offscreen snapshots has a delay of less than 500 ms. 228> 229> If a component is on a time-consuming task, for example, an **\<Image>** or **\<Web>** component that is loading online images, its loading may be still in progress when this API is called. In this case, the output snapshot does not represent the component in the way it looks when the loading is successfully completed. 230 231**System capability**: SystemCapability.ArkUI.ArkUI.Full 232 233**Parameters** 234 235| Name | Type | Mandatory | Description | 236| ------- | ---------------------------------------- | ---- | ---------- | 237| builder | [CustomBuilder](../arkui-ts/ts-types.md#custombuilder8) | Yes | Builder of the custom component.| 238 239**Return value** 240 241| Type | Description | 242| ----------------------------- | -------- | 243| Promise<image.PixelMap> | Promise used to return the result.| 244 245**Error codes** 246 247| ID | Error Message | 248| ------ | ---------------------------------------- | 249| 100001 | if builder is not a valid build function. | 250 251**Example** 252 253```ts 254import componentSnapshot from '@ohos.arkui.componentSnapshot' 255import image from '@ohos.multimedia.image' 256 257@Entry 258@Component 259struct OffscreenSnapshotExample { 260 @State pixmap: image.PixelMap | undefined = undefined 261 262 @Builder 263 RandomBuilder() { 264 Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) { 265 Text('Test menu item 1') 266 .fontSize(20) 267 .width(100) 268 .height(50) 269 .textAlign(TextAlign.Center) 270 Divider().height(10) 271 Text('Test menu item 2') 272 .fontSize(20) 273 .width(100) 274 .height(50) 275 .textAlign(TextAlign.Center) 276 }.width(100) 277 } 278 279 build() { 280 Column() { 281 Button("click to generate offscreen UI snapshot") 282 .onClick(() => { 283 componentSnapshot.createFromBuilder(this.RandomBuilder()) 284 .then((pixmap: image.PixelMap) => { 285 this.pixmap = pixmap 286 // save pixmap to file 287 // .... 288 }) 289 }) 290 }.width('80%').margin({ left: 10, top: 5, bottom: 5 }).height(200) 291 .border({ color: '#880606', width: 2 }) 292 } 293} 294``` 295