步骤 生成二维码 - 获取二维码的base64 - 获取权限 - 获得地址 – 保存
import { QRCode } from "taro-code"; const ref = useRef<any>(); <TView ref={ref}> <QRCode text='world' size={200} scale={4} errorCorrectLevel='M' typeNumber={2} /> </TView>
在外面加一层View 是为了获取taro-code
生成的image 从而获得src 也就是base64格式的二维码图片
处理权限相关操作
import { getSetting, authorize, saveImageToPhotosAlbum, getFileSystemManager, env, } from "@tarojs/taro"; const handleSaveCode = useCallback(() => { getSetting({ success: function ({ authSetting }) { //没有权限则申请 if (!authSetting["scope.writePhotosAlbum"]) { authorize({ scope: "scope.writePhotosAlbum", success: () => { //存储二维码 handleWriteFile(); }, }); } else handleWriteFile(); }, }); }, []);
handleWriteFile
存储本地逻辑
const handleWriteFile = useCallback(() => { const { src } = ref.current.childNodes[0].props; const data = src.split(",")[1]; const filePath = `${env.USER_DATA_PATH}/${Date.now()}.png`; const { writeFile } = getFileSystemManager(); // 写入 获得文件路径 writeFile({ data, filePath, encoding: "base64", success: () => { //存入相册 saveImageToPhotosAlbum({ filePath, success: () => { console.log("