HarmonyOS 鸿蒙Next如何将图片base64字符串转PixelMap?
HarmonyOS 鸿蒙Next如何将图片base64字符串转PixelMap?
如何将图片base64字符串转PixelMap?
1 回复
import { util } from '@kit.ArkTS';
import { image } from '@kit.ImageKit';
base64ToPixelMap(base64: string): Promise<image.PixelMap> {
//将原始图片base64字符串转变为通过base64字符串
const reg = new RegExp(‘data:image/\w+;base64,’);
const base64Str = base64.replace(reg, ‘’);
//将通用base64字符串转变为arrayBuffer
let base64Helper = new util.Base64Helper();
let arrayBuffer = base64Helper.decodeSync(base64Str).buffer as ArrayBuffer;
//将arrayBuffer转变为pixelMap
let imageSource = image.createImageSource(arrayBuffer);
let opts: image.DecodingOptions = { editable: false }
//注意:这里return的是Promise,因此使用时需要在业务侧拿到最终的PixelMap
return imageSource.createPixelMap(opts);
}
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>