文档管理中心

如何将PixelMap保存到相册

PixelMap使用imagePacker.packToFile()的方法将ImageSource图片源编码后直接打包进文件。

参考代码:

收起
自动换行
深色代码主题
复制
  1. import { resourceManager } from '@kit.LocalizationKit';
  2. import { photoAccessHelper } from '@kit.MediaLibraryKit';
  3. import { BusinessError } from '@kit.BasicServicesKit';
  4. import { image } from '@kit.ImageKit';
  5. import { fileIo } from '@kit.CoreFileKit';
  6. @Entry
  7. @Component
  8. struct SavePixelMapToAlbum {
  9. @State saveButtonOptions: SaveButtonOptions = {
  10. icon: SaveIconStyle.FULL_FILLED,
  11. text: SaveDescription.SAVE,
  12. buttonType: ButtonType.Capsule
  13. };
  14. @State pixel: image.PixelMap | undefined = undefined;
  15. @State albumPath: string = '';
  16. @State photoSize: number = 0;
  17. private context: Context = this.getUIContext().getHostContext()!;
  18. async aboutToAppear() {
  19. const resourceMgr: resourceManager.ResourceManager = this.context.resourceManager;
  20. // "beer.jpeg" is the name of the image file under the rawfile directory, which can be modified and used according to your own needs
  21. const fileData: Uint8Array = await resourceMgr.getRawFileContent('beer.jpeg');
  22. let buffer = new Uint8Array(fileData).buffer as object as ArrayBuffer;
  23. let imageResource = image.createImageSource(buffer);
  24. let opts: image.DecodingOptions = { editable: true };
  25. this.pixel = await imageResource.createPixelMap(opts);
  26. }
  27. async savePixelMapToAlbum() {
  28. // Obtain the save path of the album
  29. let helper = photoAccessHelper.getPhotoAccessHelper(this.context);
  30. let uri = await helper.createAsset(photoAccessHelper.PhotoType.IMAGE, 'jpeg');
  31. let file = await fileIo.open(uri, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE);
  32. let imagePackerApi = image.createImagePacker();
  33. let packOpts: image.PackingOption = { format: 'image/jpeg', quality: 98 };
  34. imagePackerApi.packToFile(this.pixel, file.fd, packOpts, (err: BusinessError) => {
  35. if (err) {
  36. console.error(`Failed to pack the image to file.code ${err.code},message is ${err.message}`);
  37. } else {
  38. console.info('Succeeded in packing the image to file.');
  39. imagePackerApi.release((err: BusinessError) => {
  40. if (err) {
  41. console.error(`Failed to release the image source instance.code ${err.code},message is ${err.message}`);
  42. } else {
  43. console.info('Succeeded in releasing the image source instance.');
  44. fileIo.close(file.fd);
  45. }
  46. })
  47. this.getUIContext().getPromptAction().showToast({ message: '已保存至相册!' });
  48. }
  49. })
  50. }
  51. build() {
  52. Row() {
  53. Column() {
  54. Image(this.pixel)
  55. .objectFit(ImageFit.None)
  56. .height('30%')
  57. SaveButton(this.saveButtonOptions)
  58. .onClick(async (event, result: SaveButtonOnClickResult) => {
  59. if (result === SaveButtonOnClickResult.SUCCESS) {
  60. this.savePixelMapToAlbum();
  61. }
  62. })
  63. }
  64. .width('100%')
  65. }
  66. .height('100%')
  67. }
  68. }
在 FAQ 中进行搜索
请输入您想要搜索的关键词