1e41f4b71Sopenharmony_ci# PhotoEditorExtensionContext
2e41f4b71Sopenharmony_ciPhotoEditorExtensionContext是PhotoEditorExtensionAbility的上下文,继承自ExtensionContext,提供PhotoEditorExtensionAbility的相关配置信息以及保存图片接口。
3e41f4b71Sopenharmony_ci> **说明:**
4e41f4b71Sopenharmony_ci> 
5e41f4b71Sopenharmony_ci> 本模块首批接口从API version 12开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
6e41f4b71Sopenharmony_ci> 
7e41f4b71Sopenharmony_ci> 本模块接口仅可在Stage模型下使用。
8e41f4b71Sopenharmony_ci> 
9e41f4b71Sopenharmony_ci> 本模块接口需要在主线程中使用,不要在Worker、TaskPool等子线程中使用。
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci## 导入模块
12e41f4b71Sopenharmony_ci```ts
13e41f4b71Sopenharmony_ciimport { common } from '@kit.AbilityKit';
14e41f4b71Sopenharmony_ci```
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ci## PhotoEditorExtensionContext.saveEditedContentWithUri
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_cisaveEditedContentWithUri(uri: string): Promise\<AbilityResult\>
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ci传入编辑过的图片的uri并保存。
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci**模型约束:** 此接口仅可在Stage模型下使用。
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Ability.AppExtension.PhotoEditorExtension
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_ci**参数:**
27e41f4b71Sopenharmony_ci| 参数名  | 类型  | 必填  | 说明  |
28e41f4b71Sopenharmony_ci| ------------ | ------------ | ------------ | ------------ |
29e41f4b71Sopenharmony_ci| uri | string  | 是  | 编辑后图片的[uri](../apis-core-file-kit/js-apis-file-fileuri.md),格式为file://\<bundleName>/\<sandboxPath>。  |
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci**返回值:**
32e41f4b71Sopenharmony_ci|  类型 | 说明  |
33e41f4b71Sopenharmony_ci| ------------ | ------------ |
34e41f4b71Sopenharmony_ci| Promise\<AbilityResult\> | Promise对象,返回AbilityResult对象,编辑过的图片uri存在want.uri中,[uri](../apis-core-file-kit/js-apis-file-fileuri.md)格式为file://\<bundleName>/\<sandboxPath>。  |
35e41f4b71Sopenharmony_ci
36e41f4b71Sopenharmony_ci**错误码:**
37e41f4b71Sopenharmony_ci
38e41f4b71Sopenharmony_ci以下错误码详细介绍参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。
39e41f4b71Sopenharmony_ci
40e41f4b71Sopenharmony_ci|  错误码ID | 错误信息  |
41e41f4b71Sopenharmony_ci| ------------ | ------------ |
42e41f4b71Sopenharmony_ci| 401  | Params error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.  |
43e41f4b71Sopenharmony_ci| 29600001  | Internal error. |
44e41f4b71Sopenharmony_ci| 29600002  |  Image input error. |
45e41f4b71Sopenharmony_ci| 29600003  |  Image too big. |
46e41f4b71Sopenharmony_ci
47e41f4b71Sopenharmony_ci**示例:**
48e41f4b71Sopenharmony_ci```ts
49e41f4b71Sopenharmony_ciimport { common, UIExtensionContentSession, Want } from '@kit.AbilityKit';
50e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit';
51e41f4b71Sopenharmony_ciimport { fileIo } from '@kit.CoreFileKit';
52e41f4b71Sopenharmony_ciimport { image } from '@kit.ImageKit';
53e41f4b71Sopenharmony_ci
54e41f4b71Sopenharmony_ciconst TAG = '[ExamplePhotoEditorAbility]';
55e41f4b71Sopenharmony_ci
56e41f4b71Sopenharmony_ci@Entry
57e41f4b71Sopenharmony_ci@Component
58e41f4b71Sopenharmony_cistruct Index {
59e41f4b71Sopenharmony_ci  // 原始图片
60e41f4b71Sopenharmony_ci  @State originalImage: PixelMap | null = null;
61e41f4b71Sopenharmony_ci
62e41f4b71Sopenharmony_ci  build() {
63e41f4b71Sopenharmony_ci    Row() {
64e41f4b71Sopenharmony_ci      Column() {
65e41f4b71Sopenharmony_ci        Button("RotateAndSaveImg").onClick(event => {
66e41f4b71Sopenharmony_ci          hilog.info(0x0000, TAG, `Start to edit image and save.`);
67e41f4b71Sopenharmony_ci
68e41f4b71Sopenharmony_ci          this.originalImage?.rotate(90).then(() => {
69e41f4b71Sopenharmony_ci            const imagePackerApi: image.ImagePacker = image.createImagePacker();
70e41f4b71Sopenharmony_ci            let packOpts: image.PackingOption = { format: "image/jpeg", quality: 98 };
71e41f4b71Sopenharmony_ci            imagePackerApi.packing(this.originalImage, packOpts).then((data: ArrayBuffer) => {
72e41f4b71Sopenharmony_ci              let context = getContext(this) as common.PhotoEditorExtensionContext;
73e41f4b71Sopenharmony_ci              let filePath = context.filesDir + "/edited.jpg";
74e41f4b71Sopenharmony_ci              let file: fileIo.File | undefined;
75e41f4b71Sopenharmony_ci              try{
76e41f4b71Sopenharmony_ci                file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE
77e41f4b71Sopenharmony_ci                | fileIo.OpenMode.CREATE | fileIo.OpenMode.TRUNC);
78e41f4b71Sopenharmony_ci                let writeLen = fileIo.writeSync(file.fd, data);
79e41f4b71Sopenharmony_ci                hilog.info(0x0000, TAG, "write data to file succeed and size is:"
80e41f4b71Sopenharmony_ci                  + writeLen);
81e41f4b71Sopenharmony_ci                fileIo.closeSync(file);
82e41f4b71Sopenharmony_ci                context.saveEditedContentWithUri(filePath).then
83e41f4b71Sopenharmony_ci                  (data => {
84e41f4b71Sopenharmony_ci                    hilog.info(0x0000, TAG,
85e41f4b71Sopenharmony_ci                      `saveContentEditingWithUri result: ${JSON.stringify(data)}`);
86e41f4b71Sopenharmony_ci                  });
87e41f4b71Sopenharmony_ci              } catch (e) {
88e41f4b71Sopenharmony_ci                hilog.info(0x0000, TAG, `writeImage failed:${e}`);
89e41f4b71Sopenharmony_ci              } finally {
90e41f4b71Sopenharmony_ci                fileIo.close(file);
91e41f4b71Sopenharmony_ci              }
92e41f4b71Sopenharmony_ci            }).catch((error: BusinessError) => {
93e41f4b71Sopenharmony_ci              hilog.error(0x0000, TAG,
94e41f4b71Sopenharmony_ci                'Failed to pack the image. And the error is: ' + String(error));
95e41f4b71Sopenharmony_ci            })
96e41f4b71Sopenharmony_ci          })
97e41f4b71Sopenharmony_ci        }).margin({ top: 10 })
98e41f4b71Sopenharmony_ci      }
99e41f4b71Sopenharmony_ci    }
100e41f4b71Sopenharmony_ci  }
101e41f4b71Sopenharmony_ci}
102e41f4b71Sopenharmony_ci```
103e41f4b71Sopenharmony_ci## PhotoEditorExtensionContext.saveEditedContentWithImage
104e41f4b71Sopenharmony_ci
105e41f4b71Sopenharmony_cisaveEditedContentWithImage(pixeMap: image.PixelMap, option: image.PackingOption): Promise\<AbilityResult\>
106e41f4b71Sopenharmony_ci
107e41f4b71Sopenharmony_ci传入编辑过的图片的PixMap对象并保存。
108e41f4b71Sopenharmony_ci
109e41f4b71Sopenharmony_ci**模型约束:** 此接口仅可在Stage模型下使用。
110e41f4b71Sopenharmony_ci
111e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Ability.AppExtension.PhotoEditorExtension
112e41f4b71Sopenharmony_ci
113e41f4b71Sopenharmony_ci**参数:**
114e41f4b71Sopenharmony_ci| 参数名  | 类型  | 必填  | 说明  |
115e41f4b71Sopenharmony_ci| ------------ | ------------ | ------------ | ------------ |
116e41f4b71Sopenharmony_ci| pixeMap | [image.PixelMap](../apis-image-kit/js-apis-image.md#pixelmap7)  | 是  | 编辑过的图片image.PixelMap。  |
117e41f4b71Sopenharmony_ci| option  | [image.PackingOption](..//apis-image-kit/js-apis-image.md#packingoption)  |  是 | 设置打包参数。  |
118e41f4b71Sopenharmony_ci
119e41f4b71Sopenharmony_ci**返回值:**
120e41f4b71Sopenharmony_ci|  类型 | 说明  |
121e41f4b71Sopenharmony_ci| ------------ | ------------ |
122e41f4b71Sopenharmony_ci| Promise\<AbilityResult\> | Promise对象,返回AbilityResult对象,编辑过的图片uri存在want.uri中,[uri](../apis-core-file-kit/js-apis-file-fileuri.md)格式为file://\<bundleName>/\<sandboxPath>。  |
123e41f4b71Sopenharmony_ci
124e41f4b71Sopenharmony_ci**错误码:**
125e41f4b71Sopenharmony_ci
126e41f4b71Sopenharmony_ci以下错误码详细介绍参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。
127e41f4b71Sopenharmony_ci
128e41f4b71Sopenharmony_ci|  错误码ID | 错误信息  |
129e41f4b71Sopenharmony_ci| ------------ | ------------ |
130e41f4b71Sopenharmony_ci| 401  | Params error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.  |
131e41f4b71Sopenharmony_ci| 29600001  | Internal error. |
132e41f4b71Sopenharmony_ci| 29600002  |  Image input error. |
133e41f4b71Sopenharmony_ci| 29600003  |  Image too big. |
134e41f4b71Sopenharmony_ci
135e41f4b71Sopenharmony_ci**示例:**
136e41f4b71Sopenharmony_ci```ts
137e41f4b71Sopenharmony_ciimport { common, UIExtensionContentSession, Want } from '@kit.AbilityKit';
138e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit';
139e41f4b71Sopenharmony_ciimport { image } from '@kit.ImageKit';
140e41f4b71Sopenharmony_ci
141e41f4b71Sopenharmony_ciconst TAG = '[ExamplePhotoEditorAbility]';
142e41f4b71Sopenharmony_ci
143e41f4b71Sopenharmony_ci@Entry
144e41f4b71Sopenharmony_ci@Component
145e41f4b71Sopenharmony_cistruct Index {
146e41f4b71Sopenharmony_ci  // 原始图片
147e41f4b71Sopenharmony_ci  @State originalImage: PixelMap | null = null;
148e41f4b71Sopenharmony_ci
149e41f4b71Sopenharmony_ci  build() {
150e41f4b71Sopenharmony_ci    Row() {
151e41f4b71Sopenharmony_ci      Column() {
152e41f4b71Sopenharmony_ci        Button("RotateAndSaveImg").onClick(event => {
153e41f4b71Sopenharmony_ci          hilog.info(0x0000, TAG, `Start to edit image and save.`);
154e41f4b71Sopenharmony_ci
155e41f4b71Sopenharmony_ci          this.originalImage?.rotate(90).then(() => {
156e41f4b71Sopenharmony_ci            let packOpts: image.PackingOption = { format: "image/jpeg", quality: 98 };
157e41f4b71Sopenharmony_ci            try {
158e41f4b71Sopenharmony_ci              let context = getContext(this) as common.PhotoEditorExtensionContext;
159e41f4b71Sopenharmony_ci              context.saveEditedContentWithImage(this.originalImage as image.PixelMap,
160e41f4b71Sopenharmony_ci                packOpts).then(data => {
161e41f4b71Sopenharmony_ci                  hilog.info(0x0000, TAG,
162e41f4b71Sopenharmony_ci                    `saveContentEditingWithImage result: ${JSON.stringify(data)}`);
163e41f4b71Sopenharmony_ci                });
164e41f4b71Sopenharmony_ci            } catch (e) {
165e41f4b71Sopenharmony_ci              hilog.error(0x0000, TAG, `saveContentEditingWithImage failed:${e}`);
166e41f4b71Sopenharmony_ci              return;
167e41f4b71Sopenharmony_ci            }
168e41f4b71Sopenharmony_ci          })
169e41f4b71Sopenharmony_ci        }).margin({ top: 10 })
170e41f4b71Sopenharmony_ci      }
171e41f4b71Sopenharmony_ci    }
172e41f4b71Sopenharmony_ci  }
173e41f4b71Sopenharmony_ci}
174e41f4b71Sopenharmony_ci```