HarmonyOS OH\_ImageSourceNative\_CreateFromData设置图片,报错\[ImageSource\]check mismatched format?

OH\_ImageSourceNative\_CreateFromData通过这个函数来设置图片,但得到这个报错是什么原因?其中data是JS侧通过ArrayBuffer传过来的。

阅读 528
1 个回答

在ArkTS中,从pixelmap获取的buffer已经是解码后的数据,不应该传入imagesource,imagesource是用来对未解码数据进行解码的。此处应该直接创建PixelMap:

void *buffer_data = NULL;
size_t byte_length = 0;
napi_get_arraybuffer_info(env, args[0], &buffer_data, &byte_length);
uint8_t* data = static_cast<uint8_t*>(buffer_data);

OH_Pixelmap_InitializationOptions *createOpts;

OH_PixelmapInitializationOptions_Create(&createOpts);
OH_PixelmapInitializationOptions_SetWidth(createOpts, 20);
OH_PixelmapInitializationOptions_SetHeight(createOpts, 20);
OH_PixelmapInitializationOptions_SetPixelFormat(createOpts, PIXEL_FORMAT_RGBA_8888);
OH_PixelmapInitializationOptions_SetAlphaType(createOpts, PIXELMAP_ALPHA_TYPE_UNKNOWN);
logo
HarmonyOS
子站问答
访问
宣传栏