如何对ArrayBuffer类型的数据刷新(HarmonyOS 鸿蒙Next)

如何对ArrayBuffer类型的数据刷新(HarmonyOS 鸿蒙Next) 1、然后把数据缓存到Array。
2、界面不会刷新,是状态管理不支持ArrayBuffer类型的吗?

2 回复
import { common } from '@kit.AbilityKit';
import { image } from '@kit.ImageKit';
import { buffer } from '@kit.ArkTS';

@ObservedV2
class TestData {
 [@Trace](/user/Trace) data?: ArrayBuffer
}

@Entry
@ComponentV2
struct V2Page {
   private context: common.UIAbilityContext  = getContext() as common.UIAbilityContext
    @Local arry: TestData []   = [];
    aboutToAppear(): void {
      let data  = this.context.resourceManager.getMediaContentSync($r("app.media.app_icon").id)
      let fileData   = new TestData()
      fileData.data  = buffer.from(data.buffer).buffer
      this.arry.push(fileData)
    }
    compress(data?:ArrayBuffer) : image.PixelMap{
      const imageSource = image.createImageSource(data);
      const  pixel = imageSource.createPixelMapSync({});
      return pixel;
    }
  build() {
    Column () {
      List(){
        ForEach(this.arry,(item:TestData)=>{
          ListItem(){
            Image(this.compress(item.data)).width(100).height(100)
          }
        })
      }
      Button("修改数组的ArryBuff数据").onClick(()=>{
        let data = this.context.resourceManager.getMediaContentSync($r("app.media.startIcon").id)
        let fileData   = this.arry[0]
        fileData.data  = buffer.from(data.buffer).buffer
      })
    }
    .height('100%')
    .width('100%')
  }
}

@Trace装饰器用于装饰类以及类中的属性,使得被装饰的类和属性具有深度观测的能力

更多关于如何对ArrayBuffer类型的数据刷新(HarmonyOS 鸿蒙Next)的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS(鸿蒙Next)中,ArrayBuffer是一种用于表示二进制数据的缓冲对象。要刷新ArrayBuffer类型的数据,通常需要重新分配或修改其内容。以下是几种常见的操作方式:

  1. 重新分配内存:可以通过创建一个新的ArrayBuffer对象来替换原有的ArrayBuffer,从而实现数据的刷新。例如:

    let buffer = new ArrayBuffer(16); // 创建一个16字节的ArrayBuffer
    buffer = new ArrayBuffer(32); // 重新分配一个32字节的ArrayBuffer
    
  2. 使用TypedArray或DataView:通过TypedArray(如Uint8ArrayInt32Array等)或DataView来操作ArrayBuffer中的数据。你可以使用这些视图来覆盖或修改ArrayBuffer中的数据。例如:

    let buffer = new ArrayBuffer(16);
    let uint8Array = new Uint8Array(buffer);
    uint8Array.fill(0); // 将buffer中的所有字节设置为0
    
  3. 清空数据:如果需要清空ArrayBuffer中的数据,可以使用TypedArrayfill方法将所有字节设置为特定值(如0)。例如:

    let buffer = new ArrayBuffer(16);
    let uint8Array = new Uint8Array(buffer);
    uint8Array.fill(0); // 将buffer中的所有字节设置为0
    
  4. 复制数据:如果需要从一个ArrayBuffer复制数据到另一个ArrayBuffer,可以使用TypedArrayset方法。例如:

    let sourceBuffer = new ArrayBuffer(16);
    let targetBuffer = new ArrayBuffer(16);
    let sourceArray = new Uint8Array(sourceBuffer);
    let targetArray = new Uint8Array(targetBuffer);
    targetArray.set(sourceArray); // 将sourceBuffer的数据复制到targetBuffer
    

这些操作可以帮助你在鸿蒙系统中有效地刷新或操作ArrayBuffer类型的数据。

回到顶部