基于 react-native-fast-image 的 OpenHarmony 适配版,高性能图片加载与缓存组件(优先加载、缓存控制)
| 文件 | 最后提交记录 | 最后更新时间 |
|---|---|---|
| 5 年前 | ||
| 6 年前 | ||
| 26 天前 | ||
| 26 天前 | ||
| 1 个月前 | ||
| 3 个月前 | ||
| 11 个月前 | ||
| 26 天前 | ||
| 9 年前 | ||
| 1 个月前 | ||
| 1 个月前 | ||
| 1 个月前 | ||
| 7 年前 | ||
| 3 个月前 | ||
| 26 天前 | ||
| 3 个月前 | ||
| 3 个月前 |
文档模板:v0.4.2
react-native-fast-image
本项目 <=8.8.0 以下的版本基于 DylanVann/react-native-fast-image 开发,>=9.0.0 的版本基于 dream-horizon-org/react-native-fast-image 开发。
该第三方库的仓库已迁移至 Gitcode,且支持直接从 npm 下载,新的包名为:@react-native-ohos/react-native-fast-image 版本所属关系如下:
| 三方库名称 | 三方库版本(npm地址) | 发布信息 | 支持RN版本 | Autolink | 编译API版本 | 社区基线版本 | 源码地址 |
|---|---|---|---|---|---|---|---|
| @react-native-ohos/react-native-fast-image | ~ 9.2.1 | Gitcode Releases | 0.82.* / 0.84.* | 是 | API12+ | 8.13.0 | master |
| @react-native-ohos/react-native-fast-image | ~ 8.8.0 | Gitcode Releases | 0.82.* | 是 | API12+ | 8.6.3 | br_rnoh0.82 |
| @react-native-ohos/react-native-fast-image | ~ 8.7.0 | Gitcode Releases | 0.77.* | 否 | API12+ | 8.6.3 | br_rnoh0.77 |
| @react-native-ohos/react-native-fast-image | ~ 8.6.4 | Gitcode Releases | 0.72.* | 是 | API12+ | 8.6.3 | br_rnoh0.72 |
| @react-native-oh-tpl/react-native-fast-image | <= 8.6.3-0.4.17@deprecated | Github Releases(deprecated) | 0.72.* | 否 | API12+ | 8.6.3 | sig |
简介
react-native-fast-image 是一个快速、高效的图片加载库,支持图片缓存、优先级加载、预加载等功能,提供了丰富的图片加载回调接口。
下载安装
进入到工程目录并输入以下命令:
npm
npm install @react-native-ohos/react-native-fast-image
yarn
yarn add @react-native-ohos/react-native-fast-image
Link
| 是否支持autolink | RN框架版本 | |
|---|---|---|
| ~ 9.2.1 | 是 | 0.82 、0.84 |
使用AutoLink的工程需要根据该文档配置,Autolink框架指导文档:https://gitcode.com/CPF-RN/ohos_react_native/blob/master/docs/zh-cn/Autolinking.md
如您使用的版本支持 Autolink,并且工程已接入 Autolink,可跳过ManualLink配置。
ManualLink: 此步骤为手动配置原生依赖项的指导
首先需要使用 DevEco Studio 打开项目里的 HarmonyOS 工程 harmony。
1. Overrides RN SDK
为了让工程依赖同一个版本的 RN SDK,需要在工程根目录的 oh-package.json5 添加 overrides 字段,指向工程需要使用的 RN SDK 版本。替换的版本既可以是一个具体的版本号,也可以是一个模糊版本,还可以是本地存在的 HAR 包或源码目录。
关于该字段的作用请阅读官方说明
{
"overrides": {
"@rnoh/react-native-openharmony": "^0.84.2" // ohpm 在线版本
// "@rnoh/react-native-openharmony" : "./react_native_openharmony.har" // 指向本地 har 包的路径
// "@rnoh/react-native-openharmony" : "./react_native_openharmony" // 指向源码路径
}
}
2. 引入原生端代码
目前有两种方法:
- 通过 har 包引入;
- 直接链接源码。
方法一:通过 har 包引入(推荐)
har 包位于三方库安装路径的 `harmony` 文件夹下。
打开 entry/oh-package.json5,添加以下依赖
"dependencies": {
"@rnoh/react-native-openharmony": "file:../react_native_openharmony",
"@react-native-ohos/react-native-fast-image": "file:../../node_modules/@react-native-ohos/react-native-fast-image/harmony/fast_image.har"
}
点击右上角的 sync 按钮
或者在命令行终端执行:
cd entry
ohpm install
方法二:直接链接源码
如需使用直接链接源码,请参考[直接链接源码说明](https://gitcode.com/CPF-RN/usage-docs/blob/master/zh-cn/link-source-code.md)
3. 配置 CMakeLists 和引入 FastImagePackage
打开 entry/src/main/cpp/CMakeLists.txt,添加:
project(rnapp)
cmake_minimum_required(VERSION 3.4.1)
set(CMAKE_SKIP_BUILD_RPATH TRUE)
set(RNOH_APP_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
set(NODE_MODULES "${CMAKE_CURRENT_SOURCE_DIR}/../../../../../node_modules")
+ set(OH_MODULES "${CMAKE_CURRENT_SOURCE_DIR}/../../../oh_modules")
set(RNOH_CPP_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../react-native-harmony/harmony/cpp")
set(LOG_VERBOSITY_LEVEL 1)
set(CMAKE_ASM_FLAGS "-Wno-error=unused-command-line-argument -Qunused-arguments")
set(CMAKE_CXX_FLAGS "-fstack-protector-strong -Wl,-z,relro,-z,now,-z,noexecstack -s -fPIE -pie")
set(WITH_HITRACE_SYSTRACE 1) # for other CMakeLists.txt files to use
add_compile_definitions(WITH_HITRACE_SYSTRACE)
add_subdirectory("${RNOH_CPP_DIR}" ./rn)
# RNOH_BEGIN: manual_package_linking_1
add_subdirectory("../../../../sample_package/src/main/cpp" ./sample-package)
+ add_subdirectory("${OH_MODULES}/@react-native-ohos/react-native-fast-image/src/main/cpp" ./fast-image)
# RNOH_END: manual_package_linking_1
file(GLOB GENERATED_CPP_FILES "./generated/*.cpp")
add_library(rnoh_app SHARED
${GENERATED_CPP_FILES}
"./PackageProvider.cpp"
"${RNOH_CPP_DIR}/RNOHAppNapiBridge.cpp"
)
target_link_libraries(rnoh_app PUBLIC rnoh)
# RNOH_BEGIN: manual_package_linking_2
target_link_libraries(rnoh_app PUBLIC rnoh_sample_package)
+ target_link_libraries(rnoh_app PUBLIC rnoh_fast_image)
# RNOH_END: manual_package_linking_2
打开 entry/src/main/cpp/PackageProvider.cpp,添加:
#include "RNOH/PackageProvider.h"
#include "generated/RNOHGeneratedPackage.h"
#include "SamplePackage.h"
+ #include "FastImagePackage.h"
using namespace rnoh;
std::vector<std::shared_ptr<Package>> PackageProvider::getPackages(Package::Context ctx) {
return {
std::make_shared<RNOHGeneratedPackage>(ctx),
std::make_shared<SamplePackage>(ctx),
+ std::make_shared<FastImagePackage>(ctx),
};
}
4. 在 ArkTs 侧引入 FastImagePackage
打开 entry/src/main/ets/RNPackagesFactory.ts,添加:
...
+ import {FastImagePackage} from '@react-native-ohos/react-native-fast-image/ts';
export function createRNPackages(ctx: RNPackageContext): RNPackage[] {
return [
new SamplePackage(ctx),
+ new FastImagePackage(ctx)
];
}
运行
点击右上角的 sync 按钮
或者在命令行终端执行:
cd entry
ohpm install
然后编译、运行即可。
约束与限制
兼容性
本文档内容基于以下版本验证通过:
- RNOH: 0.84.2; SDK: HarmonyOS 6.0.1 Release SDK; IDE: DevEco Studio 6.0.1 Release; ROM: 6.1.1(24);
权限要求
-
如果 source 使用网络 url,应用需要申请网络权限
在
entry/src/main/module.json5中添加
requestPermissions: [
{
name: "ohos.permission.INTERNET",
},
],
使用示例
下面的代码展示了这个库的基本使用场景:
使用时 import 的库名不变。
import React from "react";
import { StyleSheet, View, ScrollView } from "react-native";
import FastImage, {
ResizeMode,
OnLoadEvent,
OnProgressEvent,
} from "react-native-fast-image";
export const FastImageDemo = () => {
return (
<ScrollView>
<View>
<FastImage
style={styles.image}
source={{
uri: "https://res8.vmallres.com/pimages/uomcdn/CN/pms/202205/gbom/6941487259298/428_428_D7BFF22D4678EB68440F914B352214C4mp_tds.png",
}}
resizeMode={FastImage.resizeMode.contain}
onLoadStart={() => {
console.log("onLoadStart: success");
}}
onProgress={(e: OnProgressEvent) => {
console.log(
"onProgress: success loaded=" +
e.nativeEvent.loaded +
" total=" +
e.nativeEvent.total
);
}}
onLoad={(e: OnLoadEvent) => {
console.log(
"onLoad: success width=" +
e.nativeEvent.width +
" height=" +
e.nativeEvent.height
);
}}
onError={() => {
console.log("onError: success");
}}
onLoadEnd={() => {
console.log("onLoadEnd: success");
}}
/>
</View>
</ScrollView>
);
};
const styles = StyleSheet.create({
image: {
width: 200,
height: 200,
margin: 20,
},
});
接口说明
"Platform"列表示该属性在原三方库上支持的平台。
"HarmonyOS Support"列为 yes 表示 HarmonyOS 平台支持该属性;no 则表示不支持;partially 表示部分支持。使用方法跨平台一致,效果对标 iOS 或 Android 的效果。
属性
FastImageProps
| 名称 | 参数类型 | 默认值 | 必填 | 平台 | HarmonyOS平台支持 | 描述 |
|---|---|---|---|---|---|---|
source |
Source 或 ImageRequireSource | none | Yes | All | Yes | 必填 - 图片来源。可以通过带有 uri 属性的对象引用远程资源,也可以通过 require('./img.png') 引用本地资源 |
defaultSource |
ImageRequireSource | none | No | All | Yes | 默认占位图,通过 require 加载的本地资源 |
resizeMode |
'contain' / 'cover' / 'stretch' / 'center' | cover | No | All | Yes | 图片缩放模式 |
fallback |
boolean | false | No | All | Yes | 设置为 true 时回退使用 RN Image 组件渲染,此时会忽略 source.cache |
transition |
'fade' / 'none' | 'none' | No | All | Yes | 图片加载完成后的过渡效果,fade 为淡入 |
tintColor |
ColorValue | none | No | All | Yes | 着色。设置后所有非透明像素的颜色更改为指定颜色 |
blurRadius |
number | 0 | No | All | Yes | 高斯模糊滤镜的模糊半径 |
onLoadStart |
callback | none | No | All | Yes | 图片开始加载时调用 |
onProgress |
callback: OnProgressEvent | none | No | All | Yes | 图片加载过程中调用,参数为 {nativeEvent: {loaded, total}} |
onLoad |
callback: OnLoadEvent | none | No | All | Yes | 图片加载成功时调用,参数为 {nativeEvent: {width, height}} |
onError |
callback: OnErrorEvent | none | No | All | Yes | 图片加载失败时调用,参数为 {nativeEvent: {error}} |
onLoadEnd |
callback | none | No | All | Yes | 图片加载结束时调用 |
onLayout |
callback: LayoutChangeEvent | none | No | All | Yes | 监听尺寸 / 位置发生变化,参数为 {nativeEvent: {layout: {x, y, width, height}}} |
style |
StyleProp<ImageStyle> | none | No | All | Yes | 样式,支持 FlexStyle 及圆角、背景色等图片样式 |
testID |
string | none | No | All | Yes | UI 自动化测试的唯一标识 |
children |
React.ReactNode | none | No | All | Yes | 子元素,渲染在图片上层 |
ImageStyle 对象属性
style 参数类型为 StyleProp<ImageStyle>。ImageStyle 为 FastImage 自定义的样式接口,继承自 FlexStyle、TransformsStyle、ShadowStyleIOS,除基础布局/变换/阴影样式外,显式支持以下字段:
| 名称 | 参数类型 | 描述 |
|---|---|---|
backfaceVisibility |
'visible' / 'hidden' | 是否显示背面 |
borderBottomLeftRadius |
number | 左下角圆角半径 |
borderBottomRightRadius |
number | 右下角圆角半径 |
backgroundColor |
string | 背景色 |
borderColor |
string | 边框颜色 |
borderWidth |
number | 边框宽度 |
borderRadius |
number | 圆角半径 |
borderTopLeftRadius |
number | 左上角圆角半径 |
borderTopRightRadius |
number | 右上角圆角半径 |
overlayColor |
string | 覆盖层颜色(不支持) |
opacity |
number | 不透明度,取值 0~1 |
Source 对象属性
| 名称 | 参数类型 | 默认值 | 必填 | 平台 | HarmonyOS平台支持 | 描述 |
|---|---|---|---|---|---|---|
uri |
string | none | No | All | Yes | 加载的远程图片资源。类型定义为可选,但远程图片使用时必须提供 uri。 |
headers |
object | none | No | All | Yes | 加载图片时附带的请求头。例如:{Authorization: 'someAuthToken'} |
priority |
'low' / 'normal' / 'high' | 'normal' | No | All | Yes | 加载优先级。 |
cache |
'immutable' / 'web' / 'cacheOnly' | 'immutable' | No | All | Partially | 设置缓存模式。鸿蒙平台仅支持 'immutable' 和 'cacheOnly',不支持 'web' |
API
| 名称 | 类型 | 参数类型 | 返回值 | 必填 | 平台 | HarmonyOS平台支持 | 描述 |
|---|---|---|---|---|---|---|---|
FastImage.preload |
function | sources: Source[] | void | No | All | Yes | 预加载图片以便后续显示 |
FastImage.clearMemoryCache |
function | / | Promise<void> | No | All | Yes | 从内存缓存中清除所有图片 |
FastImage.clearDiskCache |
function | / | Promise<void> | No | All | Yes | 从磁盘缓存中清除所有图片 |
静态属性
| 名称 | 类型 | 参数类型 | 返回值 | 必填 | 平台 | HarmonyOS平台支持 | 描述 |
|---|---|---|---|---|---|---|---|
FastImage.transition |
enum | / | / | No | All | Yes | 转场动画常量 |
FastImage.resizeMode |
enum | / | / | No | All | Yes | 图片缩放模式 |
FastImage.priority |
enum | / | / | No | All | Yes | 加载优先级 |
FastImage.cacheControl |
enum | / | / | No | All | Yes | 缓存模式 |
遗留问题
无
其他
无
目录结构
/rntpc_react-native-fast-image # 项目根目录
├── harmony # 鸿蒙适配代码
│ └─ fast_image.har # har包
│ └─ fast_image # 鸿蒙适配核心代码
│ └─ index.ets # 鸿蒙适配代码入口
│ └─ ts.ets # ArkTS侧导出
│ └─ src/main/cpp # C++ 适配代码
│ └─ src/main/ets # ArkTS 适配代码
├── src # RN代码
│ └─ index.tsx # 入口文件
│ └─ FastImageViewNativeComponent.ts # Fabric 组件类型定义
│ └─ NativeFastImage.ts # TurboModule 类型定义
├── README_en.md # 英文安装使用方法
├── README.md # 中文安装使用方法
贡献代码
使用过程中发现任何问题都可以提交 Issue,当然,也非常欢迎提交 PR 。
开源协议
本项目基于 The MIT License (MIT) ,请自由地享受和参与开源。