react-native下载图片到本地相册

需求

点击右上角下载icon,可以将当前图片下载并保存到本地相册。
在这里插入图片描述
下载的图片:

在这里插入图片描述

流程

下载图片的本质其实是,

固定需要下载的页面内容和样式 ===》将其放在当前页面不可见区域 ===》点击下载按钮 ===》穿一个ref给native,会自动拉起手机系统下载到本地相册

todo:native如何下载的话需要进一步再看一下

分析一下需要下载图片的样式:
在这里插入图片描述

  • 头部logo,可选可不选
  • 底部二维码和文案,固定的展示
  • 中间部分是外部传进来的card部分

所以我们可以设置需要的props如下,

interface SnapshotProps {
   
  backgroundImage: number;
  cardNode: React.ReactNode;
  bottomOpacity?: number; // 底部bottom透明度
  isShowHeader?: boolean; // 是否需要头部logo,例如首页应该为false
  viewRef: React.MutableRefObject<null>;
}

代码

import React, {
    memo } from 'react';
import {
    Image, ImageBackground, StyleSheet, Text, View } from 'react-native';
import Shopeepay from '../../assets/Shopeepay';
import QRCode from '../../assets/qrcode.png';

interface SnapshotProps {
   
  backgroundImage: number;
  cardNode: React.ReactNode;
  bottomOpacity?: number; // 底部bottom透明度
  isShowHeader?: boolean; // 是否需要头部logo,例如首页应该为false
  viewRef: React.MutableRefObject<null>;
}

const getColorWithOpacity = (rgb: string, opacity: number): string =>
  rgb.replace(')', `, ${
     opacity})`).replace('rgb', 'rgba');

const Snapshot = ({
    
  backgroundImage,
  bottomOpacity = 1,
  cardNode,
  isShowHeader = true,
  viewRef,
}: SnapshotProps) => {
   
  return (
    <ImageBackground
      style={
   styles.bgContainer}
      source={
   backgroundImage}
      ref={
   viewRef} // viewRef关键点
    >
      {
   isShowHeader && (
        <View style={
   styles.headerView}>
          <View style={
   styles.shopeepaySvg}>
            <Shopeepay />
          </View>
        </View>
      )}
      <View style={
   styles.cardView}>{
   cardNode}</View> // 外部传进来的中间内容部分
      <View
        style={
   [
          styles.bottomView,
          {
    // 背景透明度,如果设置Opacity属性的话,其所有子元素也会是透明或不透明状态
            backgroundColor: getColorWithOpacity(
              'rgb(233,78,43)',
              bottomOpacity
            ),
          },
        ]}
      >
        <View style={
   styles.leftView}>
          <Text style={
   styles.title}>Scan to enter ShopeePay</Text>
          <Text style={
   styles.subTitle}>Shopee.com/wrapup</Text>
        </View>
        <View style={
   styles.rightView}>
          <Image style={
   styles.qrcodeImage} source={
   QRCode} />
        </View>
      </View>
    </ImageBackground>
  );
};

export default memo(Snapshot);

const styles = StyleSheet.create({
   
  bgContainer: {
   
    width: WINDOW_WIDTH,
    height: WINDOW_HEIGHT,
  },
  headerView: {
   
    justifyContent: 'center',
    alignItems: 'center',
    width: WINDOW_WIDTH,
    height: 100,
  },
  shopeepaySvg: {
   
    position: 'absolute',
    top: 50,
  },
  cardView: {
   
    flex: 1,
  },
  bottomView: {
   
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'space-between',
    paddingHorizontal: 16,
    height: 110,
  },
  leftView: {
   
    marginLeft: 18,
  },
  title: {
   
    fontSize: 16,
    fontWeight: '500',
    color: Colors.white,
  },
  subTitle: {
   
    fontSize: 14,
    marginTop: 4,
    color: Colors.white,
  },
  rightView: {
   
    flex: 1,
    marginRight: 18,
    alignItems: 'flex-end',
  },
  qrcodeImage: {
   
    width: 80,
    height: 80,
  },
});

使用:

const onDownloadPress = async () => {
   
  // ...
  const resultFile = await captureViewToImage(viewRef); // 生成一个字符串,
  if (resultFile) {
   
    setImgUri(resultFile);
    try {
   
      await storeImageToDevice(rootTag, resultFile);
    } catch (err) {
   
      // ...
    }
  }
};

<Snapshot
  viewRef={
   viewRef} // 关键点
  backgroundImage={
   WrapUpBg}
  cardNode={
   carouselData[carouselIndex]}
/>

相关推荐

  1. react native 截图并保存相册

    2024-01-09 18:10:03       23 阅读
  2. react native 相机拍照

    2024-01-09 18:10:03       36 阅读
  3. 下载图片本地,多个图片压缩后下载本地

    2024-01-09 18:10:03       50 阅读
  4. react native 下载功能实现

    2024-01-09 18:10:03       35 阅读
  5. react native 图片预览

    2024-01-09 18:10:03       33 阅读
  6. 微信小程序下载 base64 视频文件本地相册

    2024-01-09 18:10:03       68 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-09 18:10:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-09 18:10:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-01-09 18:10:03       82 阅读
  4. Python语言-面向对象

    2024-01-09 18:10:03       91 阅读

热门阅读

  1. 探索中国顶级攻击面管理工具:关键优劣势解析

    2024-01-09 18:10:03       56 阅读
  2. Springboot自定义实体类读取nacos的当中的配置

    2024-01-09 18:10:03       55 阅读
  3. spring中的单例模式

    2024-01-09 18:10:03       77 阅读
  4. 使用ss代替netstat

    2024-01-09 18:10:03       57 阅读
  5. KY23 最小花费 DP

    2024-01-09 18:10:03       60 阅读
  6. 算法:删除字符串中的所有相邻重复项

    2024-01-09 18:10:03       66 阅读
  7. Redis面试题4

    2024-01-09 18:10:03       51 阅读