feat(ContactSupportView): 添加保存图片到本地的功能
- 在 ContactSupportView 组件中添加保存图片按钮 - 实现保存图片到本地的逻辑 - 更新权限请求,添加对相册的访问权限 - 优化界面布局,调整图片显示和按钮样式
这个提交包含在:
父节点
c2425f621a
当前提交
b00fca281d
@ -6,6 +6,9 @@ import { hilog } from '@kit.PerformanceAnalysisKit';
|
|||||||
import { image } from '@kit.ImageKit';
|
import { image } from '@kit.ImageKit';
|
||||||
import { buffer } from '@kit.ArkTS';
|
import { buffer } from '@kit.ArkTS';
|
||||||
import { LogHelper } from './LogHelper';
|
import { LogHelper } from './LogHelper';
|
||||||
|
import { ToolsHelper } from './ToolsHelper';
|
||||||
|
import { photoAccessHelper } from '@kit.MediaLibraryKit';
|
||||||
|
import { GlobalContext } from '../ContextConfig';
|
||||||
|
|
||||||
export class FileHelper {
|
export class FileHelper {
|
||||||
private constructor() {
|
private constructor() {
|
||||||
@ -311,6 +314,11 @@ export class FileHelper {
|
|||||||
return fs.accessSync(path);
|
return fs.accessSync(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 图片转base64
|
||||||
|
* @param photoUri
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
static BmpToBase64(photoUri: string): Promise<string> {
|
static BmpToBase64(photoUri: string): Promise<string> {
|
||||||
const imagePackerApi: image.ImagePacker = image.createImagePacker();
|
const imagePackerApi: image.ImagePacker = image.createImagePacker();
|
||||||
const packOpts: image.PackingOption = { format: 'image/jpeg', quality: 100 };
|
const packOpts: image.PackingOption = { format: 'image/jpeg', quality: 100 };
|
||||||
@ -336,6 +344,35 @@ export class FileHelper {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 保存图片到相册,需要注意权限
|
||||||
|
* @param pixmap
|
||||||
|
*/
|
||||||
|
static async saveImage(pixmap: image.PixelMap) {
|
||||||
|
let imageBuffer: ArrayBuffer = new ArrayBuffer(1)
|
||||||
|
try {
|
||||||
|
imageBuffer = await image.createImagePacker().packing(pixmap, { format: "image/png", quality: 100 })
|
||||||
|
} catch (err) {
|
||||||
|
ToolsHelper.showMessage('保存失败')
|
||||||
|
console.error(`Invoke packingPixelMap2Jpg failed, err: ${JSON.stringify(err)}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
const uri = await photoAccessHelper.getPhotoAccessHelper(GlobalContext.getContext())
|
||||||
|
.createAsset(photoAccessHelper.PhotoType.IMAGE, 'png')
|
||||||
|
|
||||||
|
fs.open(uri, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)
|
||||||
|
.then(async (file) => {
|
||||||
|
try {
|
||||||
|
await fs.write(file.fd, imageBuffer)
|
||||||
|
ToolsHelper.showMessage('保存成功')
|
||||||
|
} finally {
|
||||||
|
await fs.close(file.fd) // 确保文件关闭
|
||||||
|
}
|
||||||
|
}).catch(() => {
|
||||||
|
ToolsHelper.showMessage('保存失败')
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 打开文件,支持使用URI打开文件。使用Promise异步回调。
|
* 打开文件,支持使用URI打开文件。使用Promise异步回调。
|
||||||
* @param path string 文件的应用沙箱路径或URI。
|
* @param path string 文件的应用沙箱路径或URI。
|
||||||
|
|||||||
正在加载...
在新工单中引用
屏蔽一个用户