加载rawfile资源图片,转换PixelMap有黑边
代码如下
const resourceMgr: resourceManager.ResourceManager =
(PHMethod.context() as common.UIAbilityContext).resourceManager;
resourceMgr.getRawFileContent(name).then((fileData: Uint8Array) => {
console.log("Succeeded in getting RawFileContent")
// 获取图片的ArrayBuffer
const buffer = fileData.buffer.slice(0);
const imageSource = image.createImageSource(buffer);
const imagePackerApi = image.createImagePacker();
let packOpts: image.PackingOption = { format: "image/jpeg", quality: quality||30 };
imagePackerApi.packing(imageSource, packOpts).then(async (data: ArrayBuffer) => {
// data 为打包获取到的文件流,写入文件保存即可得到一张图片
if (data) {
const imageSource: image.ImageSource = image.createImageSource(data);
let decodingOptions: image.DecodingOptions = {
editable: true,
desiredPixelFormat: 3,
}
resolve(imageSource.createPixelMap(decodingOptions));
}
}).catch((error: BusinessError) => {
console.error('Failed to pack the image. And the error is: ' + error);
})
因原图片是png格式 ,format格式调整为png
参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-image-V5\#packingoption