鸿蒙5.0开发进阶:ArkTS API-PhotoEditorExtensionContext

往期鸿蒙全套实战文章必看:(文中附带全栈鸿蒙学习资料)


PhotoEditorExtensionContext

PhotoEditorExtensionContext是PhotoEditorExtensionAbility的上下文,继承自ExtensionContext,提供PhotoEditorExtensionAbility的相关配置信息以及保存图片接口。

说明

本模块首批接口从API version 12开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

本模块接口仅可在Stage模型下使用。

本模块接口需要在主线程中使用,不要在Worker、TaskPool等子线程中使用。

导入模块

import { common } from '@kit.AbilityKit';

PhotoEditorExtensionContext.saveEditedContentWithUri

saveEditedContentWithUri(uri: string): Promise<AbilityResult>

传入编辑过的图片的uri并保存。

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Ability.AppExtension.PhotoEditorExtension

参数:

参数名类型必填说明
uristring编辑后图片的uri,格式为file://<bundleName>/<sandboxPath>。

返回值:

类型说明
Promise<AbilityResult>Promise对象,返回AbilityResult对象,编辑过的图片uri存在want.uri中,uri格式为file://<bundleName>/<sandboxPath>。

错误码:

以下错误码详细介绍。

错误码ID错误信息
401Params error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
29600001Internal error.
29600002Image input error.
29600003Image too big.

示例:

import { common, UIExtensionContentSession, Want } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { fileIo } from '@kit.CoreFileKit';
import { image } from '@kit.ImageKit';

const TAG = '[ExamplePhotoEditorAbility]';

@Entry
@Component
struct Index {
  // 原始图片
  @State originalImage: PixelMap | null = null;

  build() {
    Row() {
      Column() {
        Button("RotateAndSaveImg").onClick(event => {
          hilog.info(0x0000, TAG, `Start to edit image and save.`);

          this.originalImage?.rotate(90).then(() => {
            const imagePackerApi: image.ImagePacker = image.createImagePacker();
            let packOpts: image.PackingOption = { format: "image/jpeg", quality: 98 };
            imagePackerApi.packing(this.originalImage, packOpts).then((data: ArrayBuffer) => {
              let context = getContext(this) as common.PhotoEditorExtensionContext;
              let filePath = context.filesDir + "/edited.jpg";
              let file: fileIo.File | undefined;
              try{
                file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE
                | fileIo.OpenMode.CREATE | fileIo.OpenMode.TRUNC);
                let writeLen = fileIo.writeSync(file.fd, data);
                hilog.info(0x0000, TAG, "write data to file succeed and size is:"
                  + writeLen);
                fileIo.closeSync(file);
                context.saveEditedContentWithUri(filePath).then
                  (data => {
                    hilog.info(0x0000, TAG,
                      `saveContentEditingWithUri result: ${JSON.stringify(data)}`);
                  });
              } catch (e) {
                hilog.info(0x0000, TAG, `writeImage failed:${e}`);
              } finally {
                fileIo.close(file);
              }
            }).catch((error: BusinessError) => {
              hilog.error(0x0000, TAG,
                'Failed to pack the image. And the error is: ' + String(error));
            })
          })
        }).margin({ top: 10 })
      }
    }
  }
}

PhotoEditorExtensionContext.saveEditedContentWithImage

saveEditedContentWithImage(pixeMap: image.PixelMap, option: image.PackingOption): Promise<AbilityResult>

传入编辑过的图片的PixMap对象并保存。

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Ability.AppExtension.PhotoEditorExtension

参数:

参数名类型必填说明
pixeMapimage.PixelMap编辑过的图片image.PixelMap。
optionimage.PackingOption设置打包参数。

返回值:

类型说明
Promise<AbilityResult>Promise对象,返回AbilityResult对象,编辑过的图片uri存在want.uri中,uri格式为file://<bundleName>/<sandboxPath>。

错误码:

以下错误码详细介绍。

错误码ID错误信息
401Params error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
29600001Internal error.
29600002Image input error.
29600003Image too big.

示例:

import { common, UIExtensionContentSession, Want } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { image } from '@kit.ImageKit';

const TAG = '[ExamplePhotoEditorAbility]';

@Entry
@Component
struct Index {
  // 原始图片
  @State originalImage: PixelMap | null = null;

  build() {
    Row() {
      Column() {
        Button("RotateAndSaveImg").onClick(event => {
          hilog.info(0x0000, TAG, `Start to edit image and save.`);

          this.originalImage?.rotate(90).then(() => {
            let packOpts: image.PackingOption = { format: "image/jpeg", quality: 98 };
            try {
              let context = getContext(this) as common.PhotoEditorExtensionContext;
              context.saveEditedContentWithImage(this.originalImage as image.PixelMap,
                packOpts).then(data => {
                  hilog.info(0x0000, TAG,
                    `saveContentEditingWithImage result: ${JSON.stringify(data)}`);
                });
            } catch (e) {
              hilog.error(0x0000, TAG, `saveContentEditingWithImage failed:${e}`);
              return;
            }
          })
        }).margin({ top: 10 })
      }
    }
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值