• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# PhotoEditorExtensionContext
2**PhotoEditorExtensionContext**, which inherits from **ExtensionContext**, provides the context environment for the PhotoEditorExtensionAbility. It provides PhotoEditorExtensionAbility related configuration and APIs for saving images.
3> **NOTE**
4>
5> The initial APIs of this module are supported since API version 12. Newly added APIs will be marked with a superscript to indicate their earliest API version.
6>
7> The APIs of this module can be used only in the stage model.
8>
9> The APIs of this module must be used in the main thread, but not in sub-threads such as Worker and TaskPool.
10
11## Modules to Import
12```ts
13import { common } from '@kit.AbilityKit';
14```
15
16## PhotoEditorExtensionContext.saveEditedContentWithUri
17
18saveEditedContentWithUri(uri: string): Promise\<AbilityResult\>
19
20Saves an edited image, which is passed in through a URI.
21
22**Model restriction**: This API can be used only in the stage model.
23
24**System capability**: SystemCapability.Ability.AppExtension.PhotoEditorExtension
25
26**Parameters**
27| Name | Type | Mandatory | Description |
28| ------------ | ------------ | ------------ | ------------ |
29| uri | string  | Yes | [URI](../apis-core-file-kit/js-apis-file-fileuri.md) of the edited image. The format is file://\<bundleName>/\<sandboxPath>. |
30
31**Return value**
32|  Type| Description |
33| ------------ | ------------ |
34| Promise\<AbilityResult\> | Promise used to return an **AbilityResult** object. The URI of the edited image is stored in **want.uri**. The [URI](../apis-core-file-kit/js-apis-file-fileuri.md) format is file://\<bundleName>/\<sandboxPath>. |
35
36**Error codes**
37
38For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
39
40|  ID| Error Message |
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**Example**
48```ts
49import { common, UIExtensionContentSession, Want } from '@kit.AbilityKit';
50import { hilog } from '@kit.PerformanceAnalysisKit';
51import { fileIo } from '@kit.CoreFileKit';
52import { image } from '@kit.ImageKit';
53import { BusinessError } from '@kit.BasicServicesKit';
54
55const TAG = '[ExamplePhotoEditorAbility]';
56
57@Entry
58@Component
59struct Index {
60  // Original image
61  @State originalImage: PixelMap | null = null;
62
63  build() {
64    Row() {
65      Column() {
66        Button('RotateAndSaveImg').onClick(event => {
67          hilog.info(0x0000, TAG, `Start to edit image and save.`);
68
69          this.originalImage?.rotate(90).then(() => {
70            const imagePackerApi: image.ImagePacker = image.createImagePacker();
71            let packOpts: image.PackingOption = { format: 'image/jpeg', quality: 98 };
72            imagePackerApi.packToData(this.originalImage, packOpts).then((data: ArrayBuffer) => {
73              let context = getContext(this) as common.PhotoEditorExtensionContext;
74              let filePath = context.filesDir + '/edited.jpg';
75              let file: fileIo.File | undefined;
76              try{
77                file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE
78                | fileIo.OpenMode.CREATE | fileIo.OpenMode.TRUNC);
79                let writeLen = fileIo.writeSync(file.fd, data);
80                hilog.info(0x0000, TAG, 'write data to file succeed and size is:'
81                  + writeLen);
82                fileIo.closeSync(file);
83                context.saveEditedContentWithUri(filePath).then
84                  (data => {
85                    hilog.info(0x0000, TAG,
86                      `saveContentEditingWithUri result: ${JSON.stringify(data)}`);
87                  });
88              } catch (e) {
89                hilog.info(0x0000, TAG, `writeImage failed:${e}`);
90              } finally {
91                fileIo.close(file);
92              }
93            }).catch((error: BusinessError) => {
94              hilog.error(0x0000, TAG,
95                'Failed to pack the image. And the error is: ' + String(error));
96            })
97          })
98        }).margin({ top: 10 })
99      }
100    }
101  }
102}
103```
104## PhotoEditorExtensionContext.saveEditedContentWithImage
105
106saveEditedContentWithImage(pixeMap: image.PixelMap, option: image.PackingOption): Promise\<AbilityResult\>
107
108Saves an edited image, which is passed in through a **PixelMap** object.
109
110**Model restriction**: This API can be used only in the stage model.
111
112**System capability**: SystemCapability.Ability.AppExtension.PhotoEditorExtension
113
114**Parameters**
115| Name | Type | Mandatory | Description |
116| ------------ | ------------ | ------------ | ------------ |
117| pixeMap | [image.PixelMap](../apis-image-kit/js-apis-image.md#pixelmap7)  | Yes | Edited image, which is an **image.PixelMap** object. |
118| option  | [image.PackingOption](..//apis-image-kit/js-apis-image.md#packingoption)  |  Yes| Option for image packing. |
119
120**Return value**
121|  Type| Description |
122| ------------ | ------------ |
123| Promise\<AbilityResult\> | Promise used to return an **AbilityResult** object. The URI of the edited image is stored in **want.uri**. The [URI](../apis-core-file-kit/js-apis-file-fileuri.md) format is file://\<bundleName>/\<sandboxPath>. |
124
125**Error codes**
126
127For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
128
129|  ID| Error Message |
130| ------------ | ------------ |
131| 401  | Params error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.  |
132| 29600001  | Internal error. |
133| 29600002  |  Image input error. |
134| 29600003  |  Image too big. |
135
136**Example**
137```ts
138import { common, UIExtensionContentSession, Want } from '@kit.AbilityKit';
139import { hilog } from '@kit.PerformanceAnalysisKit';
140import { image } from '@kit.ImageKit';
141
142const TAG = '[ExamplePhotoEditorAbility]';
143
144@Entry
145@Component
146struct Index {
147  // Original image
148  @State originalImage: PixelMap | null = null;
149
150  build() {
151    Row() {
152      Column() {
153        Button('RotateAndSaveImg').onClick(event => {
154          hilog.info(0x0000, TAG, `Start to edit image and save.`);
155
156          this.originalImage?.rotate(90).then(() => {
157            let packOpts: image.PackingOption = { format: 'image/jpeg', quality: 98 };
158            try {
159              let context = getContext(this) as common.PhotoEditorExtensionContext;
160              context.saveEditedContentWithImage(this.originalImage as image.PixelMap,
161                packOpts).then(data => {
162                  hilog.info(0x0000, TAG,
163                    `saveContentEditingWithImage result: ${JSON.stringify(data)}`);
164                });
165            } catch (e) {
166              hilog.error(0x0000, TAG, `saveContentEditingWithImage failed:${e}`);
167              return;
168            }
169          })
170        }).margin({ top: 10 })
171      }
172    }
173  }
174}
175```
176