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'; 53import { BusinessError } from '@kit.BasicServicesKit'; 54 55const TAG = '[ExamplePhotoEditorAbility]'; 56 57@Entry 58@Component 59struct Index { 60 // 原始图片 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 = this.getUIContext().getHostContext() 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 108传入编辑过的图片的PixMap对象并保存。 109 110**模型约束:** 此接口仅可在Stage模型下使用。 111 112**系统能力:** SystemCapability.Ability.AppExtension.PhotoEditorExtension 113 114**参数:** 115| 参数名 | 类型 | 必填 | 说明 | 116| ------------ | ------------ | ------------ | ------------ | 117| pixeMap | [image.PixelMap](../apis-image-kit/js-apis-image.md#pixelmap7) | 是 | 编辑过的图片image.PixelMap。 | 118| option | [image.PackingOption](..//apis-image-kit/js-apis-image.md#packingoption) | 是 | 设置打包参数。 | 119 120**返回值:** 121| 类型 | 说明 | 122| ------------ | ------------ | 123| Promise\<AbilityResult\> | Promise对象,返回AbilityResult对象,编辑过的图片uri存在want.uri中,[uri](../apis-core-file-kit/js-apis-file-fileuri.md)格式为file://\<bundleName>/\<sandboxPath>。 | 124 125**错误码:** 126 127以下错误码详细介绍参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 128 129| 错误码ID | 错误信息 | 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**示例:** 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 // 原始图片 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 = this.getUIContext().getHostContext() 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```