我正在尝试使用TS2.8的TextDecoder接口将Uint8Array转换为字符串,以便在web中显示图像。我尝试使用的方法如下:
displayImage(image: Uint8Array): string {
var fileString = new TextDecoder("utf-8").decode(image);
return 'data:image/jpeg;base64,' + fileString;
}当我尝试编译时,我收到"'TS2304:找不到名称'TextDecoder'“。
我运行的是TS2.8,所以根据this的说法,我正在尝试的应该是使用内置接口。这是需要定义提供者的情况吗?感谢您的帮助。
编辑:下面的tsconfig.json:
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es5",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2017",
"dom"
]
}
}发布于 2018-08-17 09:02:46
首先,确保在tsconfig中包含"dom“库。如果您提供了tsconfig,则可能更容易确定。
其次,您的数据URI需要base64编码的输出,而不是解码为UTF8的JPEG字节(如果它甚至是有效的UTF8)。有关如何做到这一点,请参阅this question。
第三,如果你想将Uint8Array显示为jpeg图像,Blob可能是一个更好的方法(代码是手工编写的,没有经过测试):
var blob = new Blob([image], {
type: 'image/jpeg'
});
var url = URL.createObjectURL(blob);
return url;https://stackoverflow.com/questions/51887042
复制相似问题