HarmonyOS PixelMap类型无法从父组件传递给子组件

父组件获取到图片数据是base64,转换成Pixelmap后通过修饰器@State 和 @Prob传递给子组件,但是不能展示。

HarmonyOS
2024-12-20 15:51:02
760浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

组件动态设置pixelmap实现传递的参考Demo:

@Observed
class Params {
  imagePixelMap: image.PixelMap | undefined;

  constructor(imaagePixelMap: image.PixelMap | undefined) {
    this.imagePixelMap = imaagePixelMap;
  }
}


@Component
struct ObjectLinkChild {
  @ObjectLink pixelMapParams: Params;

  build() {
    Column() {
      Image(this.pixelMapParams.imagePixelMap).width(100).height(100)
    }
  }
}


@Builder
function imageBuilder(params: Params) {
  ObjectLinkChild({ pixelMapParams: params })
}


@Entry
@Component
struct ImageExample {
  @State imagePixelMap: image.PixelMap | undefined = undefined;
  @State pixelMapParams: Params = new Params(this.imagePixelMap);
  private baseNode: MyNodeController = new MyNodeController(this.imagePixelMap);
  wrapBuilder: WrappedBuilder<[Params]> = wrapBuilder<[Params]>(imageBuilder);

  async aboutToAppear() {
    this.imagePixelMap = await this.getPixmapFromMedia($r('app.media.app_icon'))
    let node = new BuilderNode<[Params]>(this.getUIContext());
    node.build(this.wrapBuilder, this.pixelMapParams);
    this.baseNode.setNode(node);
  }

  build() {
    Column() {
      Image(this.imagePixelMap).enableAnalyzer(true).width(200).height(200)
    }
  }

  private async getPixmapFromMedia(resource: Resource) {
    let unit8Array = await getContext(this)?.resourceManager?.getMediaContent({
      bundleName: resource.bundleName,
      moduleName: resource.moduleName,
      id: resource.id
    })
    let imageSource = image.createImageSource(unit8Array.buffer.slice(0, unit8Array.buffer.byteLength))
    let createPixelMap: image.PixelMap =
      await imageSource.createPixelMap({ desiredPixelFormat: image.PixelMapFormat.RGBA_8888 })
    await imageSource.release()
    return createPixelMap
  }
}

class MyNodeController extends NodeController {
  private rootNode: BuilderNode<[Params]> | null = null;
  private pixelMap: image.PixelMap | undefined = undefined;
  private wrapBuilder: WrappedBuilder<[Params]> = wrapBuilder(imageBuilder);

  constructor(pixelMap: image.PixelMap | undefined) {
    super();
    this.pixelMap = pixelMap;
  }

  setNode(node: BuilderNode<[Params]> | null) {
    this.rootNode = node;
  }

  makeNode(uiContext: UIContext): FrameNode | null {
    if (this.rootNode === null) {
      this.rootNode = new BuilderNode(uiContext);
      this.rootNode.build(this.wrapBuilder, new Params(this.pixelMap));
    }
    return this.rootNode.getFrameNode();
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
分享
微博
QQ
微信
回复
2024-12-20 19:10:54
相关问题
组件组件传递函数
988浏览 • 1回复 待解决
组件事件能否到传递组件
3080浏览 • 1回复 待解决
HarmonyOS TabContent无法套用在组件
1527浏览 • 1回复 待解决