HarmonyOS鸿蒙Next中有什么办法可以实现PixMap的深拷贝,除了下面这种方法

HarmonyOS鸿蒙Next中有什么办法可以实现PixMap的深拷贝,除了下面这种方法

元服务现在还不支持这种方法

![cke_179.png](https://alliance-communityfile-drcn.dbankcdn.com/FileServer/getFile/cmtybbs/775/639/145/0030086000775639145.20250324095701.56079712681629572555248940127714:50001231000000:2800:FDE105F822C433B86EF02A42E421D301E22B62E346D7B9F00300BE927EC684C6.png)

更多关于HarmonyOS鸿蒙Next中有什么办法可以实现PixMap的深拷贝,除了下面这种方法的实战教程也可以访问 https://www.itying.com/category-93-b0.html

7 回复
import { AttributeUpdater } from '@kit.ArkUI';
import { image } from '@kit.ImageKit';

@Entry
@Component
struct CacheTest {
  build() {
    Column() {
      ReusableImageComponent({
        resource: $r('app.media.background')
      })
        .width('100%')
        .height(100)
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
  }
}

class MyImageAttributeModifier extends AttributeUpdater<ImageAttribute, ImageInterface> {
  initializeModifier(instance: ImageAttribute): void {
  }
}

@Reusable
@Component
struct ReusableImageComponent {
  @Prop resource: Resource
  @State imagePixelMap: image.PixelMap | undefined = undefined;
  @State imagePixelMap2: image.PixelMap | undefined = undefined
  private modifier: MyImageAttributeModifier = new MyImageAttributeModifier();

  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.RGB_565
    })
    await imageSource.release()
    return createPixelMap
  }

  async aboutToAppear() {
    this.imagePixelMap = await this.getPixmapFromMedia($r('app.media.poster1'))
    if (this.imagePixelMap) {
      let imageinfo = this.imagePixelMap.getImageInfoSync();
      let Format = 0;
      switch (imageinfo.pixelFormat) {
        case image.PixelMapFormat.RGB_565: {
          Format = 2;
          break;
        }
        case image.PixelMapFormat.RGBA_8888: {
          Format = 4;
          break;
        }
        case image.PixelMapFormat.BGRA_8888: {
          Format = 4;
          break;
        }
        case image.PixelMapFormat.RGB_888: {
          Format = 3;
          break;
        }
        case image.PixelMapFormat.ALPHA_8: {
          Format = 1;
          break;
        }
        case image.PixelMapFormat.RGBA_F16: {
          Format = 8;
          break;
        }
        case image.PixelMapFormat.NV21: {
          Format = 1.5;
          break;
        }
        case image.PixelMapFormat.NV12: {
          Format = 1.5;
          break;
        }
        default: {
          Format = 1;
          break;
        }
      }
      let array: ArrayBuffer = new ArrayBuffer(imageinfo.size.height * imageinfo.size.width * Format);
      this.imagePixelMap.readPixelsToBuffer(array);
      this.imagePixelMap2 = image.createPixelMapSync(array, {
        srcPixelFormat: imageinfo.pixelFormat,
        pixelFormat: imageinfo.pixelFormat,
        size: { width: imageinfo.size.width, height: imageinfo.size.height }
      })
      this.imagePixelMap2.flip(true, false);
    }
  }

  build() {
    Row() {
      Column() {
        Image($r('app.media.poster1'))
          .objectFit(ImageFit.Cover)
        Text('Raw picture')
      }.width('50%').height('100%')
      .margin({right: 5})

      Column() {
        Image(this.imagePixelMap2)
          .attributeModifier(this.modifier)
          .objectFit(ImageFit.Cover)
        Text('Copy picture')
      }.width('50%').height('100%')
      .margin({left: 5})
    }
    .width('100%')
    .height('100%')
  }
}

更多关于HarmonyOS鸿蒙Next中有什么办法可以实现PixMap的深拷贝,除了下面这种方法的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


createpixmapsync这个api元服务不支持,

那只能先转成Uri,再把Uri转化成pixMap了。参考我上面发的链接。

楼主问题解决了吗?怎么解决的?

在HarmonyOS鸿蒙Next中,实现PixMap的深拷贝可以通过使用PixelMap类的copy方法。具体步骤如下:

  1. 创建源PixelMap对象。
  2. 使用PixelMapcopy方法进行深拷贝。

示例代码如下:

import image from '@ohos.multimedia.image';

let sourcePixelMap: image.PixelMap = ...; // 获取源PixelMap对象
let copiedPixelMap: image.PixelMap = sourcePixelMap.copy();

copy方法会返回一个新的PixelMap对象,该对象是源PixelMap的深拷贝。

在HarmonyOS鸿蒙Next中,除了使用PixelMap.copy()方法进行PixMap的深拷贝外,还可以通过以下方式实现:

  1. 使用PixelMap.createPixelMap()方法:通过创建一个新的PixelMap对象,并将原始PixelMap的数据复制到新对象中,实现深拷贝。

  2. 使用ImageSourcePixelMap结合:先将PixelMap转换为ImageSource,再通过ImageSource生成新的PixelMap,间接实现深拷贝。

这两种方法都能有效实现PixMap的深拷贝,具体选择取决于应用场景和性能需求。

回到顶部