// TODO 知识点:通过readPixelsToBuffer实现PixelMap的深拷贝,其中readPixelsToBuffer输出为BGRA_8888
await pm.readPixelsToBuffer(buffer);
// TODO 知识点:readPixelsToBuffer输出为BGRA_8888,此处createPixelMap需转为RGBA_8888
const opts: image.InitializationOptions = {
editable: true,
pixelFormat: image.PixelMapFormat.RGBA_8888,
size: { height: imageInfo.size.height, width: imageInfo.size.width }
};
return await image.createPixelMap(buffer, opts);
}
2. 初始化时,通过深拷贝从原始PixelMap创建目标PixelMap。源码参考[ImageDepthCopy.ets]( )。
async aboutToAppear(): Promise {
const context: Context = getContext(this);
// 获取resourceManager资源管理
const resourceMgr: resourceManager.ResourceManager = context.resourceManager;
// 获取rawfile中的图片资源
const fileData: Uint8Array = await resourceMgr.getRawFileContent(ImageCropConstants.RAWFILE_PICPATH);
// 获取图片的ArrayBuffer
const buffer = fileData.buffer.slice(fileData.byteOffset, fileData.byteLength + fileData.byteOffset);
// 保存用于恢复原图的imageSource
this.imageSource = image.createImageSource(buffer);
// TODO 知识点: 创建源图片的用于深拷贝的PixelMap,因readPixelsToBuffer输出为BGRA_8888。故此处desiredPixelFormat需为BGRA_8888
const decodingOptions: image.DecodingOptions = {
editable: false, // 是否可编辑。当取值为false时,图片不可二次编辑。
desiredPixelFormat: image.PixelMapFormat.BGRA_8888,
}
// 保存用于深拷贝的pixelMap
this.pixelMapSrc = await this.imageSource.createPixelMap(decodingOptions);
//