1e41f4b71Sopenharmony_ci# @ohos.arkui.componentSnapshot (组件截图)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci本模块提供获取组件截图的能力,包括已加载的组件的截图和没有加载的组件的截图。组件截图只能够截取组件大小的区域,如果组件的绘制超出了它的区域,或子组件的绘制超出了父组件的区域,这些在组件区域外绘制的内容不会在截图中呈现。兄弟节点堆叠在组件区域内,截图不会显示兄弟组件。
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci> **说明:**
6e41f4b71Sopenharmony_ci>
7e41f4b71Sopenharmony_ci> 本模块首批接口从 API version 10 开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8e41f4b71Sopenharmony_ci>
9e41f4b71Sopenharmony_ci>对于使用[XComponent](arkui-ts/ts-basic-components-xcomponent.md)的场景,例如:Video或者相机流媒体展示类组件,不建议使用组件截图相关接口,建议从[surface](../apis-image-kit/js-apis-image.md#imagecreatepixelmapfromsurface11)直接获取图片。
10e41f4b71Sopenharmony_ci>
11e41f4b71Sopenharmony_ci>如果组件自身内容不能填满组件大小区域,那么剩余位置截图返回的内容为透明像素。如果组件使用了[图像效果](arkui-ts/ts-universal-attributes-image-effect.md)类属性或其他的效果类属性,则可能产生非用户预期的截图结果。请排查是否需要填充组件透明内容区域,或使用[窗口截图](js-apis-window.md#snapshot9)替代。
12e41f4b71Sopenharmony_ci>
13e41f4b71Sopenharmony_ci> 示例效果请以真机运行为准,当前 IDE 预览器不支持。
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ci## 导入模块
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ci```ts
19e41f4b71Sopenharmony_ciimport { componentSnapshot } from '@kit.ArkUI';
20e41f4b71Sopenharmony_ci```
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci## componentSnapshot.get
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ciget(id: string, callback: AsyncCallback<image.PixelMap>, options?: SnapshotOptions): void
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_ci获取已加载的组件的截图,传入组件的[组件标识](arkui-ts/ts-universal-attributes-component-id.md#组件标识),找到对应组件进行截图。通过回调返回结果。
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ci> **说明:** 
29e41f4b71Sopenharmony_ci>
30e41f4b71Sopenharmony_ci> 截图会获取最近一帧的绘制内容。如果在组件触发更新的同时调用截图,更新的渲染内容不会被截取到,截图会返回上一帧的绘制内容。
31e41f4b71Sopenharmony_ci
32e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
33e41f4b71Sopenharmony_ci
34e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full
35e41f4b71Sopenharmony_ci
36e41f4b71Sopenharmony_ci**参数:**
37e41f4b71Sopenharmony_ci
38e41f4b71Sopenharmony_ci| 参数名      | 类型                                  | 必填   | 说明                                       |
39e41f4b71Sopenharmony_ci| -------- | ----------------------------------- | ---- | ---------------------------------------- |
40e41f4b71Sopenharmony_ci| id       | string                              | 是    | 目标组件的[组件标识](arkui-ts/ts-universal-attributes-component-id.md#组件标识) |
41e41f4b71Sopenharmony_ci| callback | [AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)&lt;image.[PixelMap](../apis-image-kit/js-apis-image.md#pixelmap7)&gt; | 是    | 截图返回结果的回调。                               |
42e41f4b71Sopenharmony_ci| options<sup>12+</sup>       | [SnapshotOptions](#snapshotoptions12)                              | 否    | 截图相关的自定义参数。 |
43e41f4b71Sopenharmony_ci
44e41f4b71Sopenharmony_ci**错误码:** 
45e41f4b71Sopenharmony_ci
46e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)错误码。
47e41f4b71Sopenharmony_ci
48e41f4b71Sopenharmony_ci| 错误码ID | 错误信息            |
49e41f4b71Sopenharmony_ci| -------- | ------------------- |
50e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed.   |
51e41f4b71Sopenharmony_ci| 100001   | Invalid ID. |
52e41f4b71Sopenharmony_ci
53e41f4b71Sopenharmony_ci**示例:**
54e41f4b71Sopenharmony_ci
55e41f4b71Sopenharmony_ci> **说明:**
56e41f4b71Sopenharmony_ci> 
57e41f4b71Sopenharmony_ci> 直接使用componentSnapshot可能导致实例不明确的问题,建议使用[getUIContext](js-apis-arkui-UIContext.md#uicontext)获取UIContext实例,并使用[getComponentSnapshot](js-apis-arkui-UIContext.md#getcomponentsnapshot12)获取绑定实例的componentSnapshot。
58e41f4b71Sopenharmony_ci
59e41f4b71Sopenharmony_ci```ts
60e41f4b71Sopenharmony_ciimport { componentSnapshot } from '@kit.ArkUI';
61e41f4b71Sopenharmony_ciimport { image } from '@kit.ImageKit';
62e41f4b71Sopenharmony_ci
63e41f4b71Sopenharmony_ci@Entry
64e41f4b71Sopenharmony_ci@Component
65e41f4b71Sopenharmony_cistruct SnapshotExample {
66e41f4b71Sopenharmony_ci  @State pixmap: image.PixelMap | undefined = undefined
67e41f4b71Sopenharmony_ci
68e41f4b71Sopenharmony_ci  build() {
69e41f4b71Sopenharmony_ci    Column() {
70e41f4b71Sopenharmony_ci      Row() {
71e41f4b71Sopenharmony_ci        Image(this.pixmap).width(200).height(200).border({ color: Color.Black, width: 2 }).margin(5)
72e41f4b71Sopenharmony_ci        Image($r('app.media.img')).autoResize(true).width(200).height(200).margin(5).id("root")
73e41f4b71Sopenharmony_ci      }
74e41f4b71Sopenharmony_ci      Button("click to generate UI snapshot")
75e41f4b71Sopenharmony_ci        .onClick(() => {
76e41f4b71Sopenharmony_ci          // 建议使用this.getUIContext().getComponentSnapshot().get()
77e41f4b71Sopenharmony_ci          componentSnapshot.get("root", (error: Error, pixmap: image.PixelMap) => {
78e41f4b71Sopenharmony_ci            if (error) {
79e41f4b71Sopenharmony_ci              console.log("error: " + JSON.stringify(error))
80e41f4b71Sopenharmony_ci              return;
81e41f4b71Sopenharmony_ci            }
82e41f4b71Sopenharmony_ci            this.pixmap = pixmap
83e41f4b71Sopenharmony_ci          }, {scale : 2, waitUntilRenderFinished : true})
84e41f4b71Sopenharmony_ci        }).margin(10)
85e41f4b71Sopenharmony_ci    }
86e41f4b71Sopenharmony_ci    .width('100%')
87e41f4b71Sopenharmony_ci    .height('100%')
88e41f4b71Sopenharmony_ci    .alignItems(HorizontalAlign.Center)
89e41f4b71Sopenharmony_ci  }
90e41f4b71Sopenharmony_ci}
91e41f4b71Sopenharmony_ci```
92e41f4b71Sopenharmony_ci
93e41f4b71Sopenharmony_ci![componentget](figures/componentget.gif) 
94e41f4b71Sopenharmony_ci
95e41f4b71Sopenharmony_ci## componentSnapshot.get
96e41f4b71Sopenharmony_ci
97e41f4b71Sopenharmony_ciget(id: string, options?: SnapshotOptions): Promise<image.PixelMap>
98e41f4b71Sopenharmony_ci
99e41f4b71Sopenharmony_ci获取已加载的组件的截图,传入组件的[组件标识](arkui-ts/ts-universal-attributes-component-id.md#组件标识),找到对应组件进行截图。通过Promise返回结果。
100e41f4b71Sopenharmony_ci
101e41f4b71Sopenharmony_ci> **说明:**
102e41f4b71Sopenharmony_ci>
103e41f4b71Sopenharmony_ci> 截图会获取最近一帧的绘制内容。如果在组件触发更新的同时调用截图,更新的渲染内容不会被截取到,截图会返回上一帧的绘制内容。
104e41f4b71Sopenharmony_ci
105e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
106e41f4b71Sopenharmony_ci
107e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full
108e41f4b71Sopenharmony_ci
109e41f4b71Sopenharmony_ci**参数:**
110e41f4b71Sopenharmony_ci
111e41f4b71Sopenharmony_ci| 参数名  | 类型     | 必填   | 说明                                       |
112e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ---------------------------------------- |
113e41f4b71Sopenharmony_ci| id   | string | 是    | 目标组件的[组件标识](arkui-ts/ts-universal-attributes-component-id.md#组件标识) |
114e41f4b71Sopenharmony_ci| options<sup>12+</sup>       | [SnapshotOptions](#snapshotoptions12)                              | 否    | 截图相关的自定义参数。 |
115e41f4b71Sopenharmony_ci
116e41f4b71Sopenharmony_ci**返回值:**
117e41f4b71Sopenharmony_ci
118e41f4b71Sopenharmony_ci| 类型                            | 说明       |
119e41f4b71Sopenharmony_ci| ----------------------------- | -------- |
120e41f4b71Sopenharmony_ci| Promise&lt;image.[PixelMap](../apis-image-kit/js-apis-image.md#pixelmap7)&gt; | 截图返回的结果。 |
121e41f4b71Sopenharmony_ci
122e41f4b71Sopenharmony_ci**错误码:** 
123e41f4b71Sopenharmony_ci
124e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)错误码。
125e41f4b71Sopenharmony_ci
126e41f4b71Sopenharmony_ci| 错误码ID  | 错误信息                |
127e41f4b71Sopenharmony_ci| ------ | ------------------- |
128e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed.   |
129e41f4b71Sopenharmony_ci| 100001 | Invalid ID. |
130e41f4b71Sopenharmony_ci
131e41f4b71Sopenharmony_ci**示例:**
132e41f4b71Sopenharmony_ci
133e41f4b71Sopenharmony_ci> **说明:**
134e41f4b71Sopenharmony_ci> 
135e41f4b71Sopenharmony_ci> 直接使用componentSnapshot可能导致实例不明确的问题,建议使用[getUIContext](js-apis-arkui-UIContext.md#uicontext)获取UIContext实例,并使用[getComponentSnapshot](js-apis-arkui-UIContext.md#getcomponentsnapshot12)获取绑定实例的componentSnapshot。
136e41f4b71Sopenharmony_ci
137e41f4b71Sopenharmony_ci```ts
138e41f4b71Sopenharmony_ciimport { componentSnapshot } from '@kit.ArkUI';
139e41f4b71Sopenharmony_ciimport { image } from '@kit.ImageKit';
140e41f4b71Sopenharmony_ci
141e41f4b71Sopenharmony_ci@Entry
142e41f4b71Sopenharmony_ci@Component
143e41f4b71Sopenharmony_cistruct SnapshotExample {
144e41f4b71Sopenharmony_ci  @State pixmap: image.PixelMap | undefined = undefined
145e41f4b71Sopenharmony_ci
146e41f4b71Sopenharmony_ci  build() {
147e41f4b71Sopenharmony_ci    Column() {
148e41f4b71Sopenharmony_ci      Row() {
149e41f4b71Sopenharmony_ci        Image(this.pixmap).width(200).height(200).border({ color: Color.Black, width: 2 }).margin(5)
150e41f4b71Sopenharmony_ci        Image($r('app.media.img')).autoResize(true).width(200).height(200).margin(5).id("root")
151e41f4b71Sopenharmony_ci      }
152e41f4b71Sopenharmony_ci      Button("click to generate UI snapshot")
153e41f4b71Sopenharmony_ci        .onClick(() => {
154e41f4b71Sopenharmony_ci          // 建议使用this.getUIContext().getComponentSnapshot().get()
155e41f4b71Sopenharmony_ci          componentSnapshot.get("root", {scale : 2, waitUntilRenderFinished : true})
156e41f4b71Sopenharmony_ci            .then((pixmap: image.PixelMap) => {
157e41f4b71Sopenharmony_ci              this.pixmap = pixmap
158e41f4b71Sopenharmony_ci            }).catch((err:Error) => {
159e41f4b71Sopenharmony_ci            console.log("error: " + err)
160e41f4b71Sopenharmony_ci          })
161e41f4b71Sopenharmony_ci        }).margin(10)
162e41f4b71Sopenharmony_ci    }
163e41f4b71Sopenharmony_ci    .width('100%')
164e41f4b71Sopenharmony_ci    .height('100%')
165e41f4b71Sopenharmony_ci    .alignItems(HorizontalAlign.Center)
166e41f4b71Sopenharmony_ci  }
167e41f4b71Sopenharmony_ci}
168e41f4b71Sopenharmony_ci```
169e41f4b71Sopenharmony_ci
170e41f4b71Sopenharmony_ci![componentget](figures/componentget.gif) 
171e41f4b71Sopenharmony_ci
172e41f4b71Sopenharmony_ci## componentSnapshot.createFromBuilder
173e41f4b71Sopenharmony_ci
174e41f4b71Sopenharmony_cicreateFromBuilder(builder: CustomBuilder, callback: AsyncCallback<image.PixelMap>, delay?: number, checkImageStatus?: boolean, options?: SnapshotOptions): void
175e41f4b71Sopenharmony_ci
176e41f4b71Sopenharmony_ci在应用后台渲染CustomBuilder自定义组件,并输出其截图。通过回调返回结果并支持在回调中获取离屏组件绘制区域坐标和大小。
177e41f4b71Sopenharmony_ci
178e41f4b71Sopenharmony_ci> **说明:** 
179e41f4b71Sopenharmony_ci>
180e41f4b71Sopenharmony_ci> 由于需要等待组件构建、渲染成功,离屏截图的回调有500ms以内的延迟。
181e41f4b71Sopenharmony_ci>
182e41f4b71Sopenharmony_ci> 部分执行耗时任务的组件可能无法及时在截图前加载完成,因此会截取不到加载成功后的图像。例如:加载网络图片的[Image](arkui-ts/ts-basic-components-image.md)组件、[Web](../apis-arkweb/ts-basic-components-web.md)组件。
183e41f4b71Sopenharmony_ci
184e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
185e41f4b71Sopenharmony_ci
186e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full
187e41f4b71Sopenharmony_ci
188e41f4b71Sopenharmony_ci**参数:**
189e41f4b71Sopenharmony_ci
190e41f4b71Sopenharmony_ci| 参数名      | 类型                                       | 必填   | 说明         |
191e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ---- | ---------- |
192e41f4b71Sopenharmony_ci| builder  | [CustomBuilder](arkui-ts/ts-types.md#custombuilder8) | 是    | 自定义组件构建函数。<br/>**说明:** 不支持全局builder。|
193e41f4b71Sopenharmony_ci| callback | [AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)&lt;image.[PixelMap](../apis-image-kit/js-apis-image.md#pixelmap7)&gt;      | 是    | 截图返回结果的回调。支持在回调中获取离屏组件绘制区域坐标和大小。 |
194e41f4b71Sopenharmony_ci| delay<sup>12+</sup>   | number | 否    | 指定触发截图指令的延迟时间。当布局中使用了图片组件时,需要指定延迟时间,以便系统解码图片资源。资源越大,解码需要的时间越长,建议尽量使用不需要解码的PixelMap资源。<br/> 当使用PixelMap资源或对Image组件设置syncload为true时,可以配置delay为0,强制不等待触发截图。该延迟时间并非指接口从调用到返回的时间,由于系统需要对传入的builder进行临时离屏构建,因此返回的时间通常要比该延迟时间长。<br/>**说明:** 截图接口传入的builder中,不应使用状态变量控制子组件的构建,如果必须要使用,在调用截图接口时,也不应再有变化,以避免出现截图不符合预期的情况。<br/> 默认值:300 <br/> 单位:毫秒|
195e41f4b71Sopenharmony_ci| checkImageStatus<sup>12+</sup>  | boolean | 否    | 指定是否允许在截图之前,校验图片解码状态。如果为true,则会在截图之前检查所有Image组件是否已经解码完成,如果没有完成检查,则会放弃截图并返回异常。<br/>默认值:false|
196e41f4b71Sopenharmony_ci| options<sup>12+</sup>       | [SnapshotOptions](#snapshotoptions12)           | 否    | 截图相关的自定义参数。 |
197e41f4b71Sopenharmony_ci
198e41f4b71Sopenharmony_ci**错误码:** 
199e41f4b71Sopenharmony_ci
200e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)错误码。
201e41f4b71Sopenharmony_ci
202e41f4b71Sopenharmony_ci| 错误码ID | 错误信息                                                     |
203e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ |
204e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. |
205e41f4b71Sopenharmony_ci| 100001   | The builder is not a valid build function.                   |
206e41f4b71Sopenharmony_ci| 160001   | An image component in builder is not ready for taking a snapshot. The check for the ready state is required when the checkImageStatus option is enabled. |
207e41f4b71Sopenharmony_ci
208e41f4b71Sopenharmony_ci**示例:**
209e41f4b71Sopenharmony_ci
210e41f4b71Sopenharmony_ci> **说明:**
211e41f4b71Sopenharmony_ci> 
212e41f4b71Sopenharmony_ci> 直接使用componentSnapshot可能导致实例不明确的问题,建议使用[getUIContext](js-apis-arkui-UIContext.md#uicontext)获取UIContext实例,并使用[getComponentSnapshot](js-apis-arkui-UIContext.md#getcomponentsnapshot12)获取绑定实例的componentSnapshot。
213e41f4b71Sopenharmony_ci
214e41f4b71Sopenharmony_ci```ts
215e41f4b71Sopenharmony_ciimport { componentSnapshot } from '@kit.ArkUI';
216e41f4b71Sopenharmony_ciimport { image } from '@kit.ImageKit';
217e41f4b71Sopenharmony_ci
218e41f4b71Sopenharmony_ci@Entry
219e41f4b71Sopenharmony_ci@Component
220e41f4b71Sopenharmony_cistruct OffscreenSnapshotExample {
221e41f4b71Sopenharmony_ci  @State pixmap: image.PixelMap | undefined = undefined
222e41f4b71Sopenharmony_ci
223e41f4b71Sopenharmony_ci  @Builder
224e41f4b71Sopenharmony_ci  RandomBuilder() {
225e41f4b71Sopenharmony_ci    Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) {
226e41f4b71Sopenharmony_ci      Text('Test menu item 1')
227e41f4b71Sopenharmony_ci        .fontSize(20)
228e41f4b71Sopenharmony_ci        .width(100)
229e41f4b71Sopenharmony_ci        .height(50)
230e41f4b71Sopenharmony_ci        .textAlign(TextAlign.Center)
231e41f4b71Sopenharmony_ci      Divider().height(10)
232e41f4b71Sopenharmony_ci      Text('Test menu item 2')
233e41f4b71Sopenharmony_ci        .fontSize(20)
234e41f4b71Sopenharmony_ci        .width(100)
235e41f4b71Sopenharmony_ci        .height(50)
236e41f4b71Sopenharmony_ci        .textAlign(TextAlign.Center)
237e41f4b71Sopenharmony_ci    }
238e41f4b71Sopenharmony_ci    .width(100)
239e41f4b71Sopenharmony_ci    .id("builder")
240e41f4b71Sopenharmony_ci  }
241e41f4b71Sopenharmony_ci
242e41f4b71Sopenharmony_ci  build() {
243e41f4b71Sopenharmony_ci    Column() {
244e41f4b71Sopenharmony_ci      Button("click to generate offscreen UI snapshot")
245e41f4b71Sopenharmony_ci        .onClick(() => {
246e41f4b71Sopenharmony_ci          // 建议使用this.getUIContext().getComponentSnapshot().createFromBuilder()
247e41f4b71Sopenharmony_ci          componentSnapshot.createFromBuilder(()=>{this.RandomBuilder()},
248e41f4b71Sopenharmony_ci            (error: Error, pixmap: image.PixelMap) => {
249e41f4b71Sopenharmony_ci              if(error){
250e41f4b71Sopenharmony_ci                console.log("error: " + JSON.stringify(error))
251e41f4b71Sopenharmony_ci                return;
252e41f4b71Sopenharmony_ci              }
253e41f4b71Sopenharmony_ci              this.pixmap = pixmap
254e41f4b71Sopenharmony_ci              // save pixmap to file
255e41f4b71Sopenharmony_ci              // ....
256e41f4b71Sopenharmony_ci              // get component size and location
257e41f4b71Sopenharmony_ci              let info = this.getUIContext().getComponentUtils().getRectangleById("builder")
258e41f4b71Sopenharmony_ci              console.log(info.size.width + ' ' + info.size.height + ' ' + info.localOffset.x + ' ' + info.localOffset.y + ' ' + info.windowOffset.x + ' ' + info.windowOffset.y)
259e41f4b71Sopenharmony_ci            }, 320, true, {scale : 2, waitUntilRenderFinished : true})
260e41f4b71Sopenharmony_ci        })
261e41f4b71Sopenharmony_ci      Image(this.pixmap)
262e41f4b71Sopenharmony_ci        .margin(10)
263e41f4b71Sopenharmony_ci        .height(200)
264e41f4b71Sopenharmony_ci        .width(200)
265e41f4b71Sopenharmony_ci        .border({ color: Color.Black, width: 2 })
266e41f4b71Sopenharmony_ci    }.width('100%').margin({ left: 10, top: 5, bottom: 5 }).height(300)
267e41f4b71Sopenharmony_ci  }
268e41f4b71Sopenharmony_ci}
269e41f4b71Sopenharmony_ci```
270e41f4b71Sopenharmony_ci
271e41f4b71Sopenharmony_ci![componentcreate](figures/componentcreate.gif) 
272e41f4b71Sopenharmony_ci
273e41f4b71Sopenharmony_ci## componentSnapshot.createFromBuilder
274e41f4b71Sopenharmony_ci
275e41f4b71Sopenharmony_cicreateFromBuilder(builder: CustomBuilder, delay?: number, checkImageStatus?: boolean, options?: SnapshotOptions): Promise<image.PixelMap>
276e41f4b71Sopenharmony_ci
277e41f4b71Sopenharmony_ci在应用后台渲染CustomBuilder自定义组件,并输出其截图。通过Promise返回结果并支持获取离屏组件绘制区域坐标和大小。
278e41f4b71Sopenharmony_ci
279e41f4b71Sopenharmony_ci> **说明:** 
280e41f4b71Sopenharmony_ci>
281e41f4b71Sopenharmony_ci> 由于需要等待组件构建、渲染成功,离屏截图的回调有500ms以内的延迟。
282e41f4b71Sopenharmony_ci>
283e41f4b71Sopenharmony_ci> 部分执行耗时任务的组件可能无法及时在截图前加载完成,因此会截取不到加载成功后的图像。例如:加载网络图片的[Image](arkui-ts/ts-basic-components-image.md)组件、[Web](../apis-arkweb/ts-basic-components-web.md)组件。
284e41f4b71Sopenharmony_ci
285e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
286e41f4b71Sopenharmony_ci
287e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full
288e41f4b71Sopenharmony_ci
289e41f4b71Sopenharmony_ci**参数:**
290e41f4b71Sopenharmony_ci
291e41f4b71Sopenharmony_ci| 参数名     | 类型                                       | 必填   | 说明         |
292e41f4b71Sopenharmony_ci| ------- | ---------------------------------------- | ---- | ---------- |
293e41f4b71Sopenharmony_ci| builder | [CustomBuilder](arkui-ts/ts-types.md#custombuilder8) | 是    | 自定义组件构建函数。<br/>**说明:** 不支持全局builder。 |
294e41f4b71Sopenharmony_ci| delay<sup>12+</sup>   | number | 否    | 指定触发截图指令的延迟时间。当布局中使用了图片组件时,需要指定延迟时间,以便系统解码图片资源。资源越大,解码需要的时间越长,建议尽量使用不需要解码的PixelMap资源。<br/> 当使用PixelMap资源或对Image组件设置syncload为true时,可以配置delay为0,强制不等待触发截图。该延迟时间并非指接口从调用到返回的时间,由于系统需要对传入的builder进行临时离屏构建,因此返回的时间通常要比该延迟时间长。<br/>**说明:** 截图接口传入的builder中,不应使用状态变量控制子组件的构建,如果必须要使用,在调用截图接口时,也不应再有变化,以避免出现截图不符合预期的情况。<br/> 默认值:300 <br/> 单位:毫秒|
295e41f4b71Sopenharmony_ci| checkImageStatus<sup>12+</sup>  | boolean | 否    | 指定是否允许在截图之前,校验图片解码状态。如果为true,则会在截图之前检查所有Image组件是否已经解码完成,如果没有完成检查,则会放弃截图并返回异常。<br/>默认值:false|
296e41f4b71Sopenharmony_ci| options<sup>12+</sup>       | [SnapshotOptions](#snapshotoptions12)           | 否    | 截图相关的自定义参数。 |
297e41f4b71Sopenharmony_ci
298e41f4b71Sopenharmony_ci**返回值:**
299e41f4b71Sopenharmony_ci
300e41f4b71Sopenharmony_ci| 类型                            | 说明       |
301e41f4b71Sopenharmony_ci| ----------------------------- | -------- |
302e41f4b71Sopenharmony_ci| Promise&lt;image.[PixelMap](../apis-image-kit/js-apis-image.md#pixelmap7)&gt; | 截图返回的结果。 |
303e41f4b71Sopenharmony_ci
304e41f4b71Sopenharmony_ci**错误码:** 
305e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)错误码。
306e41f4b71Sopenharmony_ci
307e41f4b71Sopenharmony_ci| 错误码ID  | 错误信息                                     |
308e41f4b71Sopenharmony_ci| ------ | ---------------------------------------- |
309e41f4b71Sopenharmony_ci| 401     | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed.   |
310e41f4b71Sopenharmony_ci| 100001 | The builder is not a valid build function. |
311e41f4b71Sopenharmony_ci| 160001 | An image component in builder is not ready for taking a snapshot. The check for the ready state is required when the checkImageStatus option is enabled. |
312e41f4b71Sopenharmony_ci
313e41f4b71Sopenharmony_ci**示例:**
314e41f4b71Sopenharmony_ci
315e41f4b71Sopenharmony_ci> **说明:**
316e41f4b71Sopenharmony_ci> 
317e41f4b71Sopenharmony_ci> 直接使用componentSnapshot可能导致实例不明确的问题,建议使用[getUIContext](js-apis-arkui-UIContext.md#uicontext)获取UIContext实例,并使用[getComponentSnapshot](js-apis-arkui-UIContext.md#getcomponentsnapshot12)获取绑定实例的componentSnapshot。
318e41f4b71Sopenharmony_ci
319e41f4b71Sopenharmony_ci```ts
320e41f4b71Sopenharmony_ciimport { componentSnapshot } from '@kit.ArkUI'
321e41f4b71Sopenharmony_ciimport { image } from '@kit.ImageKit'
322e41f4b71Sopenharmony_ci
323e41f4b71Sopenharmony_ci@Entry
324e41f4b71Sopenharmony_ci@Component
325e41f4b71Sopenharmony_cistruct OffscreenSnapshotExample {
326e41f4b71Sopenharmony_ci  @State pixmap: image.PixelMap | undefined = undefined
327e41f4b71Sopenharmony_ci
328e41f4b71Sopenharmony_ci  @Builder
329e41f4b71Sopenharmony_ci  RandomBuilder() {
330e41f4b71Sopenharmony_ci    Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) {
331e41f4b71Sopenharmony_ci      Text('Test menu item 1')
332e41f4b71Sopenharmony_ci        .fontSize(20)
333e41f4b71Sopenharmony_ci        .width(100)
334e41f4b71Sopenharmony_ci        .height(50)
335e41f4b71Sopenharmony_ci        .textAlign(TextAlign.Center)
336e41f4b71Sopenharmony_ci      Divider().height(10)
337e41f4b71Sopenharmony_ci      Text('Test menu item 2')
338e41f4b71Sopenharmony_ci        .fontSize(20)
339e41f4b71Sopenharmony_ci        .width(100)
340e41f4b71Sopenharmony_ci        .height(50)
341e41f4b71Sopenharmony_ci        .textAlign(TextAlign.Center)
342e41f4b71Sopenharmony_ci    }
343e41f4b71Sopenharmony_ci    .width(100)
344e41f4b71Sopenharmony_ci    .id("builder")
345e41f4b71Sopenharmony_ci  }
346e41f4b71Sopenharmony_ci
347e41f4b71Sopenharmony_ci  build() {
348e41f4b71Sopenharmony_ci    Column() {
349e41f4b71Sopenharmony_ci      Button("click to generate offscreen UI snapshot")
350e41f4b71Sopenharmony_ci        .onClick(() => {
351e41f4b71Sopenharmony_ci          // 建议使用this.getUIContext().getComponentSnapshot().createFromBuilder()
352e41f4b71Sopenharmony_ci          componentSnapshot.createFromBuilder(()=>{this.RandomBuilder()}, 320, true, {scale : 2, waitUntilRenderFinished : true})
353e41f4b71Sopenharmony_ci            .then((pixmap: image.PixelMap) => {
354e41f4b71Sopenharmony_ci              this.pixmap = pixmap
355e41f4b71Sopenharmony_ci              // save pixmap to file
356e41f4b71Sopenharmony_ci              // ....
357e41f4b71Sopenharmony_ci              // get component size and location
358e41f4b71Sopenharmony_ci              let info = this.getUIContext().getComponentUtils().getRectangleById("builder")
359e41f4b71Sopenharmony_ci              console.log(info.size.width + ' ' + info.size.height + ' ' + info.localOffset.x + ' ' + info.localOffset.y + ' ' + info.windowOffset.x + ' ' + info.windowOffset.y)
360e41f4b71Sopenharmony_ci            }).catch((err:Error) => {
361e41f4b71Sopenharmony_ci            console.log("error: " + err)
362e41f4b71Sopenharmony_ci          })
363e41f4b71Sopenharmony_ci        })
364e41f4b71Sopenharmony_ci      Image(this.pixmap)
365e41f4b71Sopenharmony_ci        .margin(10)
366e41f4b71Sopenharmony_ci        .height(200)
367e41f4b71Sopenharmony_ci        .width(200)
368e41f4b71Sopenharmony_ci        .border({ color: Color.Black, width: 2 })
369e41f4b71Sopenharmony_ci    }.width('100%').margin({ left: 10, top: 5, bottom: 5 }).height(300)
370e41f4b71Sopenharmony_ci  }
371e41f4b71Sopenharmony_ci}
372e41f4b71Sopenharmony_ci```
373e41f4b71Sopenharmony_ci
374e41f4b71Sopenharmony_ci![componentcreate](figures/componentcreate.gif) 
375e41f4b71Sopenharmony_ci
376e41f4b71Sopenharmony_ci## componentSnapshot.getSync<sup>12+</sup>
377e41f4b71Sopenharmony_ci
378e41f4b71Sopenharmony_cigetSync(id: string, options?: SnapshotOptions): image.PixelMap
379e41f4b71Sopenharmony_ci
380e41f4b71Sopenharmony_ci获取已加载的组件的截图,传入组件的[组件标识](arkui-ts/ts-universal-attributes-component-id.md#组件标识),找到对应组件进行截图。同步等待截图完成返回[PixelMap](../apis-image-kit/js-apis-image.md#pixelmap7)。
381e41f4b71Sopenharmony_ci
382e41f4b71Sopenharmony_ci> **说明:**
383e41f4b71Sopenharmony_ci>
384e41f4b71Sopenharmony_ci> 截图会获取最近一帧的绘制内容。如果在组件触发更新的同时调用截图,更新的渲染内容不会被截取到,截图会返回上一帧的绘制内容。
385e41f4b71Sopenharmony_ci
386e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
387e41f4b71Sopenharmony_ci
388e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full
389e41f4b71Sopenharmony_ci
390e41f4b71Sopenharmony_ci**参数:**
391e41f4b71Sopenharmony_ci
392e41f4b71Sopenharmony_ci| 参数名  | 类型     | 必填   | 说明                                       |
393e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ---------------------------------------- |
394e41f4b71Sopenharmony_ci| id   | string | 是    | 目标组件的[组件标识](arkui-ts/ts-universal-attributes-component-id.md#组件标识) |
395e41f4b71Sopenharmony_ci| options       | [SnapshotOptions](#snapshotoptions12)                              | 否    | 截图相关的自定义参数。 |
396e41f4b71Sopenharmony_ci
397e41f4b71Sopenharmony_ci**返回值:**
398e41f4b71Sopenharmony_ci
399e41f4b71Sopenharmony_ci| 类型                            | 说明       |
400e41f4b71Sopenharmony_ci| ----------------------------- | -------- |
401e41f4b71Sopenharmony_ci| image.[PixelMap](../apis-image-kit/js-apis-image.md#pixelmap7) | 截图返回的结果。 |
402e41f4b71Sopenharmony_ci
403e41f4b71Sopenharmony_ci**错误码:** 
404e41f4b71Sopenharmony_ci
405e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)错误码。
406e41f4b71Sopenharmony_ci
407e41f4b71Sopenharmony_ci| 错误码ID  | 错误信息                |
408e41f4b71Sopenharmony_ci| ------ | ------------------- |
409e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed.   |
410e41f4b71Sopenharmony_ci| 100001 | Invalid ID. |
411e41f4b71Sopenharmony_ci| 160002 | Timeout. |
412e41f4b71Sopenharmony_ci
413e41f4b71Sopenharmony_ci**示例:**
414e41f4b71Sopenharmony_ci
415e41f4b71Sopenharmony_ci> **说明:**
416e41f4b71Sopenharmony_ci> 
417e41f4b71Sopenharmony_ci> 直接使用componentSnapshot可能导致实例不明确的问题,建议使用[getUIContext](js-apis-arkui-UIContext.md#uicontext)获取UIContext实例,并使用[getComponentSnapshot](js-apis-arkui-UIContext.md#getcomponentsnapshot12)获取绑定实例的componentSnapshot。
418e41f4b71Sopenharmony_ci
419e41f4b71Sopenharmony_ci```ts
420e41f4b71Sopenharmony_ciimport { componentSnapshot } from '@kit.ArkUI';
421e41f4b71Sopenharmony_ciimport { image } from '@kit.ImageKit';
422e41f4b71Sopenharmony_ci
423e41f4b71Sopenharmony_ci@Entry
424e41f4b71Sopenharmony_ci@Component
425e41f4b71Sopenharmony_cistruct SnapshotExample {
426e41f4b71Sopenharmony_ci  @State pixmap: image.PixelMap | undefined = undefined
427e41f4b71Sopenharmony_ci
428e41f4b71Sopenharmony_ci  build() {
429e41f4b71Sopenharmony_ci    Column() {
430e41f4b71Sopenharmony_ci      Row() {
431e41f4b71Sopenharmony_ci        Image(this.pixmap).width(200).height(200).border({ color: Color.Black, width: 2 }).margin(5)
432e41f4b71Sopenharmony_ci        Image($r('app.media.img')).autoResize(true).width(200).height(200).margin(5).id("root")
433e41f4b71Sopenharmony_ci      }
434e41f4b71Sopenharmony_ci      Button("click to generate UI snapshot")
435e41f4b71Sopenharmony_ci        .onClick(() => {
436e41f4b71Sopenharmony_ci          try {
437e41f4b71Sopenharmony_ci          // 建议使用this.getUIContext().getComponentSnapshot().getSync()
438e41f4b71Sopenharmony_ci            let pixelmap = componentSnapshot.getSync("root", {scale : 2, waitUntilRenderFinished : true})
439e41f4b71Sopenharmony_ci            this.pixmap = pixelmap
440e41f4b71Sopenharmony_ci          } catch (error) {
441e41f4b71Sopenharmony_ci            console.error("getSync errorCode: " + error.code + " message: " + error.message)
442e41f4b71Sopenharmony_ci          }
443e41f4b71Sopenharmony_ci        }).margin(10)
444e41f4b71Sopenharmony_ci    }
445e41f4b71Sopenharmony_ci    .width('100%')
446e41f4b71Sopenharmony_ci    .height('100%')
447e41f4b71Sopenharmony_ci    .alignItems(HorizontalAlign.Center)
448e41f4b71Sopenharmony_ci  }
449e41f4b71Sopenharmony_ci}
450e41f4b71Sopenharmony_ci```
451e41f4b71Sopenharmony_ci
452e41f4b71Sopenharmony_ci![componentget](figures/componentget.gif) 
453e41f4b71Sopenharmony_ci
454e41f4b71Sopenharmony_ci## SnapshotOptions<sup>12+</sup>
455e41f4b71Sopenharmony_ci
456e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full
457e41f4b71Sopenharmony_ci
458e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
459e41f4b71Sopenharmony_ci
460e41f4b71Sopenharmony_ci| 名称           | 类型             | 必填           | 说明                         |
461e41f4b71Sopenharmony_ci| ---------------|------------     | -----------------------------| -----------------------------|
462e41f4b71Sopenharmony_ci| scale           | number | 否 | 指定截图时图形侧绘制pixelmap的缩放比例,比例过大时截图时间会变长,或者截图可能会失败。 <br/> 默认值:1 <br/>**说明:** <br/>请不要截取过大尺寸的图片,截图不建议超过屏幕尺寸的大小。当要截取的图片目标长宽超过底层限制时,截图会返回失败,不同设备的底层限制不同。    |
463e41f4b71Sopenharmony_ci| waitUntilRenderFinished    | boolean | 否 | 指定是否强制等待系统执行截图指令前所有绘制指令都执行完成之后再截图。该选项可尽可能确保截图内容是最新的状态,应尽量开启,要注意的是,开启后接口可能需要更长的时间返回,具体的时间依赖页面当时时刻需要重绘区域的多少。<br/> 默认值:false         |