中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
微信扫码分享
@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(); } }