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