chenquansheng

fix |> 修复稿件详情查看大图缺少图片缓存的问题

@@ -7,8 +7,10 @@ import { WindowSizeManager } from '../utils/Managers'; @@ -7,8 +7,10 @@ import { WindowSizeManager } from '../utils/Managers';
7 import { runWithAnimation } from '../utils/FuncUtils'; 7 import { runWithAnimation } from '../utils/FuncUtils';
8 import { PhotoListBean } from 'wdBean/Index'; 8 import { PhotoListBean } from 'wdBean/Index';
9 import { router } from '@kit.ArkUI'; 9 import { router } from '@kit.ArkUI';
10 -import { taskpool } from '@kit.ArkTS'; 10 +import { buffer, taskpool,util } from '@kit.ArkTS';
11 import { httpRequest } from '../utils/httpRequest'; 11 import { httpRequest } from '../utils/httpRequest';
  12 +import { ImageKnifeComponent } from '@ohos/imageknife';
  13 +import { Logger,SPHelper } from 'wdKit';
12 14
13 const TAG = 'MultiPictureDetailItemComponent'; 15 const TAG = 'MultiPictureDetailItemComponent';
14 16
@@ -50,10 +52,32 @@ export struct MultiPictureDetailItemComponent { @@ -50,10 +52,32 @@ export struct MultiPictureDetailItemComponent {
50 @State currentOffset: number = 0 52 @State currentOffset: number = 0
51 @Consume duration: number 53 @Consume duration: number
52 windowSizeManager: WindowSizeManager = new WindowSizeManager(); 54 windowSizeManager: WindowSizeManager = new WindowSizeManager();
  55 + private picPathKey:string = ''
53 56
  57 + async aboutToAppear() {
54 58
55 - aboutToAppear() {  
56 this.imageUri = this.MultiPictureDetailItem.picPath 59 this.imageUri = this.MultiPictureDetailItem.picPath
  60 +
  61 + let lastIndexOfSlash = this.imageUri.lastIndexOf('/')
  62 + let lastIndexOfDot = this.imageUri.lastIndexOf('.')
  63 + let resultString = ''
  64 + if (lastIndexOfSlash != -1 && lastIndexOfDot != -1 && lastIndexOfDot >lastIndexOfSlash ) {
  65 + resultString = this.imageUri.substring(lastIndexOfSlash+1,lastIndexOfDot)
  66 + }
  67 + this.picPathKey = resultString;
  68 +
  69 + let base64Str = SPHelper.default.getSync(this.picPathKey, '') as string
  70 + let base64StartIndex = base64Str.indexOf("base64,") + "base64,".length;
  71 + base64Str = base64Str.substring(base64StartIndex);
  72 +
  73 + if (base64Str.length > 0) {
  74 + let helper = new util.Base64Helper();
  75 + let buffer: ArrayBuffer = helper.decodeSync(base64Str, util.Type.MIME).buffer as ArrayBuffer;
  76 + let imageSource = image.createImageSource(buffer);
  77 + let opts: image.DecodingOptions = { editable: true };
  78 + this.imagePixelMap = await imageSource.createPixelMap(opts);
  79 +
  80 + } else {
57 // 通过任务池(taskpool)从网络下载图片资源 81 // 通过任务池(taskpool)从网络下载图片资源
58 taskpool.execute(getPicture, this.imageUri).then((res) => { 82 taskpool.execute(getPicture, this.imageUri).then((res) => {
59 const imgBuffer = res as ArrayBuffer 83 const imgBuffer = res as ArrayBuffer
@@ -61,7 +85,21 @@ export struct MultiPictureDetailItemComponent { @@ -61,7 +85,21 @@ export struct MultiPictureDetailItemComponent {
61 this.transcodePixelMap(imgBuffer); 85 this.transcodePixelMap(imgBuffer);
62 }) 86 })
63 } 87 }
  88 + }
  89 +
  90 + async saveImage(data: image.PixelMap){
64 91
  92 + if (this.picPathKey.length > 0) {
  93 + // 转换成base64
  94 + const imagePackerApi: image.ImagePacker = image.createImagePacker();
  95 + let packOpts: image.PackingOption = { format: 'image/jpeg', quality: 100 };
  96 + imagePackerApi.packing(data, packOpts).then((data: ArrayBuffer) => {
  97 + let buf: buffer.Buffer = buffer.from(data);
  98 + let base64 = 'data:image/jpeg;base64,' + buf.toString('base64', 0, buf.length);
  99 + SPHelper.default.saveSync(this.picPathKey,base64)
  100 + })
  101 + }
  102 + }
65 103
66 /** 104 /**
67 * 使用createPixelMap将ArrayBuffer类型的图片装换为PixelMap类型 105 * 使用createPixelMap将ArrayBuffer类型的图片装换为PixelMap类型
@@ -97,6 +135,7 @@ export struct MultiPictureDetailItemComponent { @@ -97,6 +135,7 @@ export struct MultiPictureDetailItemComponent {
97 }); 135 });
98 imageSource.createPixelMap().then((data: image.PixelMap) => { 136 imageSource.createPixelMap().then((data: image.PixelMap) => {
99 this.imagePixelMap = data; 137 this.imagePixelMap = data;
  138 + this.saveImage(data);
100 }).catch((err: BusinessError) => { 139 }).catch((err: BusinessError) => {
101 console.error(`[error][createPixelMap]${err.message}`); 140 console.error(`[error][createPixelMap]${err.message}`);
102 }); 141 });