模板版本:v0.2.2
[!TIP] Github 地址
请到三方库的 Releases 发布地址查看配套的版本信息:@react-native-oh-tpl/react-native-image-base64 Releases,并下载适用版本的 tgz 包。
进入到工程目录并输入以下命令:
[!TIP] # 处替换为 tgz 包的路径
npm install @react-native-oh-tpl/react-native-image-base64@file:#yarn add @react-native-oh-tpl/react-native-image-base64@file:#下面的代码展示了这个库的基本使用场景:
[!WARNING] 使用时 import 的库名不变。
import { StyleSheet, View, Button, Image } from "react-native";
import React from "react";
import ImgToBase64 from "react-native-image-base64";
import { isSourceFile } from "typescript";
export default function PermissionsExample() {
const viewPer = React.useRef < View > null;
const ref = React.useRef(null);
const onCapture = (uri: any) => {
console.info("onCapture callback");
setTxt(JSON.stringify(uri));
};
const onCaptureFailure = (err: any) => {
console.info("onCaptureFailure " + JSON.stringify(err));
setTxt(JSON.stringify(err));
};
const [txt, setTxt] = React.useState("");
return (
<View style={styles.sectionContainer}>
<Button
title="获取网络图片的Base64编码"
onPress={async () => {
let base64Str = await ImgToBase64.getBase64String(
"https://img0.baidu.com/it/u=2303734322,1592927702&fm=253&fmt=auto&app=138&f=PNG?w=500&h=500"
);
console.info("Image's Base64:", base64Str);
}}
style={styles.button}
/>
{/* 两按钮之间的垂直距离 */}
<View style={styles.buttonSpacing} />
<Button
title="获取本地图片的Base64编码"
onPress={async () => {
let base64Str = await ImgToBase64.getBase64String("");
console.info("Image's Base64:", base64Str);
}}
style={styles.button}
/>
</View>
);
}
const styles = StyleSheet.create({
sectionContainer: {
marginTop: 100,
paddingHorizontal: 24,
},
view: {
width: "100%",
display: "flex",
flexDirection: "row",
justifyContent: "space-evenly",
flexWrap: "wrap",
margin: 50,
},
button: {
matiginVertical: 10, // 垂直间距
},
buttonSpacing: {
height: 20,
width: "100%",
},
});本库已经适配了 Codegen ,在使用前需要主动执行生成三方库桥接代码,详细请参考 Codegen 使用文档。
目前 HarmonyOS 暂不支持 AutoLink,所以 Link 步骤需要手动配置。
首先需要使用 DevEco Studio 打开项目里的 HarmonyOS 工程 harmony
{
...
"overrides": {
"@rnoh/react-native-openharmony" : "./react_native_openharmony"
}
}目前有两种方法:
- 通过 har 包引入(在 IDE 完善相关功能后该方法会被遗弃,目前首选此方法);
- 直接链接源码。
方法一:通过 har 包引入(推荐)
[!TIP] har 包位于三方库安装路径的
harmony文件夹下。
打开 entry/oh-package.json5,添加以下依赖
"dependencies": {
"@rnoh/react-native-openharmony": "file:../react_native_openharmony",
"@react-native-oh-tpl/react-native-image-base64": "file:../../node_modules/@react-native-oh-tpl/react-native-image-base64/harmony/ImageBase64.har"
}点击右上角的 sync 按钮
或者在终端执行:
cd entry
ohpm install方法二:直接链接源码
[!TIP] 如需使用直接链接源码,请参考直接链接源码说明
打开 entry/src/main/ets/RNPackagesFactory.ts,添加:
...
+ import { RNImageBase64Package } from "@react-native-oh-tpl/react-native-image-base64/ts"
export function createRNPackages(ctx: RNPackageContext): RNPackage[] {
return [
new SamplePackage(ctx),
+ new RNImageBase64Package(ctx)
];
}点击右上角的 sync 按钮
或者在终端执行:
cd entry
ohpm install然后编译、运行即可。
本文档内容基于以下版本验证通过:
- RNOH: 0.72.27; SDK: HarmonyOS-NEXT-DB1(api12) 5.0.0.22; IDE: DevEco Studio 5.0.3.300SP2;
[!tip] "Platform"列表示该属性在原三方库上支持的平台。
[!tip] "HarmonyOS Support"列为 yes 表示 HarmonyOS 平台支持该属性;no 则表示不支持;partially 表示部分支持。使用方法跨平台一致,效果对标 iOS 或 Android 的效果。
| Name | Description | Type | Required | Platform | HarmonyOS Support |
|---|---|---|---|---|---|
| getBase64String | Retrieve the base64 encoding of the specified image. Return a string type. | function | no | All | yes |
| getPixelMapFromURL | Convert network images to PixelMap format. | function | no | All | yes |
| getPixelMapFromFile | Convert local images to PixelMap format. | function | no | All | yes |
| pixelMapToBase64 | Convert PixelMap format images to their corresponding base64 encoding format. | function | no | All | yes |
本项目基于 The MIT License (MIT) ,请自由地享受和参与开源。