在研发过程中,需要将沙盒中的图片文件转为image.PixelMap进行二次编辑,系统没有提供直接的转换方法,需要进行二次封装,代码如下:
export function sandBoxImageToImagePixelMap(sandBoxFilePath: string): PixelMap {
let pathUri = fileUri.getUriFromPath(sandBoxFilePath);
const file = fileIo.openSync(pathUri);
const imageSource: image.ImageSource = image.createImageSource(file.fd);
fileIo.closeSync(file);
const imageInfo: image.ImageInfo = imageSource.getImageInfoSync();
const height = imageInfo.size.height;
const width = imageInfo.size.width;
const options: image.DecodingOptions = {
editable: true,
desiredSize: { height, width }
};
const pixelMap: PixelMap = imageSource.createPixelMapSync(options);
return pixelMap;
}使用示例如下:
let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
let dir = context.filesDir;
let pngPath = dir + "/xxx.png";
let imagePixelMap = sandBoxImageToImagePixelMap(pngPath);
















