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

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

HarmonyOS
2天前
浏览
收藏 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();
  }
}
分享
微博
QQ
微信
回复
2天前
相关问题
组件组件传递函数
292浏览 • 1回复 待解决
组件事件能否到传递组件
2413浏览 • 1回复 待解决
HarmonyOS TabContent无法套用在组件
358浏览 • 1回复 待解决
HarmonyOS Tabs组件组件问题
437浏览 • 1回复 待解决
弹窗组件调用父组件函数传递
1038浏览 • 1回复 待解决