HarmonyOS pixelMap值怎么在父子组件之间同步

HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zxjiu
@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天前
相关问题
arkts父子组件组件怎么通信传啊?
5393浏览 • 1回复 待解决
ArkTS自定义组件如何父子间传
404浏览 • 1回复 待解决
ArkTS中如何进行页面之间的传
174浏览 • 0回复 待解决
关于处理父子组件间的事件传递方式
462浏览 • 1回复 待解决
PixelMap怎么保存成图片文件
407浏览 • 1回复 待解决
HarmonyOS 页面反向传怎么传?
114浏览 • 1回复 待解决