HarmonyOS鸿蒙Next中有什么办法可以实现PixMap的深拷贝,除了下面这种方法
HarmonyOS鸿蒙Next中有什么办法可以实现PixMap的深拷贝,除了下面这种方法
元服务现在还不支持这种方法

更多关于HarmonyOS鸿蒙Next中有什么办法可以实现PixMap的深拷贝,除了下面这种方法的实战教程也可以访问 https://www.itying.com/category-93-b0.html
import { AttributeUpdater } from '@kit.ArkUI';
import { image } from '@kit.ImageKit';
@Entry
@Component
struct CacheTest {
build() {
Column() {
ReusableImageComponent({
resource: $r('app.media.background')
})
.width('100%')
.height(100)
}
.width('100%')
.height('100%')
.justifyContent(FlexAlign.Center)
}
}
class MyImageAttributeModifier extends AttributeUpdater<ImageAttribute, ImageInterface> {
initializeModifier(instance: ImageAttribute): void {
}
}
@Reusable
@Component
struct ReusableImageComponent {
@Prop resource: Resource
@State imagePixelMap: image.PixelMap | undefined = undefined;
@State imagePixelMap2: image.PixelMap | undefined = undefined
private modifier: MyImageAttributeModifier = new MyImageAttributeModifier();
private async getPixmapFromMedia(resource: Resource) {
let unit8Array = await getContext(this)?.resourceManager?.getMediaContent({
bundleName: resource.bundleName,
moduleName: resource.moduleName,
id: resource.id
})
let imageSource = image.createImageSource(unit8Array.buffer.slice(0, unit8Array.buffer.byteLength))
let createPixelMap: image.PixelMap = await imageSource.createPixelMap({
desiredPixelFormat: image.PixelMapFormat.RGB_565
})
await imageSource.release()
return createPixelMap
}
async aboutToAppear() {
this.imagePixelMap = await this.getPixmapFromMedia($r('app.media.poster1'))
if (this.imagePixelMap) {
let imageinfo = this.imagePixelMap.getImageInfoSync();
let Format = 0;
switch (imageinfo.pixelFormat) {
case image.PixelMapFormat.RGB_565: {
Format = 2;
break;
}
case image.PixelMapFormat.RGBA_8888: {
Format = 4;
break;
}
case image.PixelMapFormat.BGRA_8888: {
Format = 4;
break;
}
case image.PixelMapFormat.RGB_888: {
Format = 3;
break;
}
case image.PixelMapFormat.ALPHA_8: {
Format = 1;
break;
}
case image.PixelMapFormat.RGBA_F16: {
Format = 8;
break;
}
case image.PixelMapFormat.NV21: {
Format = 1.5;
break;
}
case image.PixelMapFormat.NV12: {
Format = 1.5;
break;
}
default: {
Format = 1;
break;
}
}
let array: ArrayBuffer = new ArrayBuffer(imageinfo.size.height * imageinfo.size.width * Format);
this.imagePixelMap.readPixelsToBuffer(array);
this.imagePixelMap2 = image.createPixelMapSync(array, {
srcPixelFormat: imageinfo.pixelFormat,
pixelFormat: imageinfo.pixelFormat,
size: { width: imageinfo.size.width, height: imageinfo.size.height }
})
this.imagePixelMap2.flip(true, false);
}
}
build() {
Row() {
Column() {
Image($r('app.media.poster1'))
.objectFit(ImageFit.Cover)
Text('Raw picture')
}.width('50%').height('100%')
.margin({right: 5})
Column() {
Image(this.imagePixelMap2)
.attributeModifier(this.modifier)
.objectFit(ImageFit.Cover)
Text('Copy picture')
}.width('50%').height('100%')
.margin({left: 5})
}
.width('100%')
.height('100%')
}
}
更多关于HarmonyOS鸿蒙Next中有什么办法可以实现PixMap的深拷贝,除了下面这种方法的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
createpixmapsync这个api元服务不支持,
那只能先转成Uri,再把Uri转化成pixMap了。参考我上面发的链接。
楼主问题解决了吗?怎么解决的?
在HarmonyOS鸿蒙Next中,实现PixMap的深拷贝可以通过使用PixelMap
类的copy
方法。具体步骤如下:
- 创建源
PixelMap
对象。 - 使用
PixelMap
的copy
方法进行深拷贝。
示例代码如下:
import image from '@ohos.multimedia.image';
let sourcePixelMap: image.PixelMap = ...; // 获取源PixelMap对象
let copiedPixelMap: image.PixelMap = sourcePixelMap.copy();
copy
方法会返回一个新的PixelMap
对象,该对象是源PixelMap
的深拷贝。
在HarmonyOS鸿蒙Next中,除了使用PixelMap.copy()
方法进行PixMap的深拷贝外,还可以通过以下方式实现:
-
使用
PixelMap.createPixelMap()
方法:通过创建一个新的PixelMap
对象,并将原始PixelMap
的数据复制到新对象中,实现深拷贝。 -
使用
ImageSource
和PixelMap
结合:先将PixelMap
转换为ImageSource
,再通过ImageSource
生成新的PixelMap
,间接实现深拷贝。
这两种方法都能有效实现PixMap的深拷贝,具体选择取决于应用场景和性能需求。