HarmonyOS 页面绘制出图片保存到相册的demo?

类似html中的canvas,根据代码区域生成海报图片这类的图片,然后保存到手机相册中

阅读 536
1 个回答

请参考示例:

import { componentSnapshot } from '@kit.ArkUI';
import { image } from '@kit.ImageKit';
import { photoAccessHelper } from '@kit.MediaLibraryKit';
import fs from '@ohos.file.fs';
import { BusinessError } from '@kit.BasicServicesKit';
const context = getContext(this)
@Entry
@Component
struct ComponentSnapshotPage {
  @State message: string = 'Hello World';

  @State pixmap:image.PixelMap |null = null
  build() {
    Column(){
      buildPic()
    }
    .height('100%')
    .width('100%')
  }
}
@Builder
function buildPic() {
  Column() {
    Column() {
      Column() {
        Image($r('app.media.app_icon'))
          .width(100)
          .height(100)
          .margin({
            top: 16,
            left: 18
          })
      }
      .alignItems(HorizontalAlign.Start)
      .width('100%')

      Text('拍卖密码封条')
        .fontSize(18)
        .lineHeight(25)
        .fontColor(Color.White)
        .margin({
          top: 11,
        })
    }
    .width('100%')
    .height(100)
    .borderRadius({
      topLeft: 4,
      topRight: 4,
    })
    .backgroundImageSize(ImageSize.Cover)

    Column() {
      Text('leftText: "投标号", rightText: "bidCardInfo.cardNumber"')
      Text('leftText: "密码, rightText: "bidCardInfo.password"')
      Text('leftText: "有效期, rightText: "bidCardInfo.cardExpireDate"')
      Divider()
        .width('100%')
        .height(1)
        .backgroundColor('#F0F0F0')
        .margin({
          top: 10,
          bottom: 10,
        })
    }
    .width('100%')
    .padding({
      top: 10,
      left: 18,
      right: 18,
      bottom: 10,
    })

  }
  .width(311)
  .id('hello')
  .backgroundColor(Color.White)
  .borderRadius(4)


  Column() {
    SaveButton({ text: SaveDescription.SAVE, buttonType: ButtonType.Capsule})
      .fontSize(16)
      .fontColor('#FFFFFF')
      .backgroundColor('#2C58D0')
      .width('100%')
      .height(40)

      .onClick(async (event: ClickEvent, result: SaveButtonOnClickResult) => {
        if (result == SaveButtonOnClickResult.SUCCESS) {
          //将图片保存到相册
          componentSnapshot.get('hello', async (error: Error, pixmap: image.PixelMap) => {
            let packOpts: image.PackingOption = { format: "image/jpeg", quality: 100 };
            const imagePackerApi = image.createImagePacker();
            imagePackerApi.packing(pixmap, packOpts).then(async (buffer: ArrayBuffer) => {
              try {
                let helper = photoAccessHelper.getPhotoAccessHelper(context)
                let uri = await helper.createAsset(photoAccessHelper.PhotoType.IMAGE, 'jpg')
                let file = await fs.open(uri, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)
                await fs.write(file.fd, buffer);
                // 关闭文件
                await fs.close(file.fd);
              } catch (error) {
                console.error("保存失败:" + JSON.stringify(error))
              }
            }).catch((error: BusinessError) => {
              console.error('保存失败,失败原因: ' + error);
            })
          })
        }
      })
  }
  .padding({
    top: 20,
    left: 18,
    right: 18,
    bottom: 20,
  })
  .width('100%')
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进