• 【Taro+react 保存图片到相册】


    import Taro from '@tarojs/taro';
    import { View, Image, Text } from '@tarojs/components';
    import IconFont from '@/components/iconfont';
    import { toast, getImg } from '@/utils/util';
    import './index.scss';
    
    class ShareVip extends Taro.PureComponent {
        constructor(props) {
            super(props);
        }
        config = {
            navigationBarTitleText: '分享得会员',
            navigationBarTextStyle: 'black'
        }
        state = {
            imgUrl: getImg('/group1/M00/00/5D/wKgBZF_KCPuEbKO3AAAAAO_XMrc004.png'),
            isOpen: false
        }
        onOpen() {
            this.setState({
                isOpen: true
            });
        }
        onDialogCancel() {
            this.setState({
                isOpen: false
            });
        }
        async shareToFriend() {
            // 后端说图片写死的,保存到本地
            Taro.getSetting({
                complete() {}
            }).then(res => {
                if (res.authSetting['scope.writePhotosAlbum']) {
                    Taro.getImageInfo({
                        src: this.state.imgUrl,
                        success(result) {
                            if (result.path) {
                                Taro.saveImageToPhotosAlbum({
                                    filePath: result.path
                                }).then(getImageInfoResult => {
                                    if (getImageInfoResult.errMsg === 'saveImageToPhotosAlbum:ok') {
                                            toast('已成功保存至相册!');
                                        } else {
                                            toast('图片保存失败!');
                                        }
                                });
                            }
                        }
                    });
                } else {
                    Taro.authorize({
                        scope: 'scope.writePhotosAlbum',
                    }).then(() => {
                        Taro.getImageInfo({
                            src: this.state.imgUrl,
                            success(result) {
                                if (result.path) {
                                    Taro.saveImageToPhotosAlbum({
                                        filePath: result.path
                                    }).then(getImageInfoResult => {
                                        if (getImageInfoResult.errMsg === 'saveImageToPhotosAlbum:ok') {
                                            toast('已成功保存至相册!');
                                        } else {
                                            toast('图片保存失败!');
                                        }
                                    });
                                }
                            }
                        });
                    });
                }
            }).catch(() => {});
        }
        render() {
            const { imgUrl, isOpen } = this.state;
            return (
                <View className='shareVip-html'>
                    <View className='shareVip-wrapper' style={{ background: `url(${getImg('/group1/M00/00/47/wKgBZF-2Oy-EDOo1AAAAAGPehpk488.png')})`, backgroundSize: '100% 100%'}}>
                    <View className='shareVip-box'>
                        <View className='shareVip-box-label'>
                            1、点击<Text>立即分享</Text>将图片分享至微信朋友圈
                        </View>
                        <Image src={getImg('/group1/M00/00/47/wKgBZF-3V0aEU1OpAAAAAEKiKWI724.png')} className='shareBtn' onClick={this.onOpen} />
                         <View className='shareVip-box-label'>
                            2、将分享页面截图发送给客服即可免费领取<Text>价值18888会员</Text>
                        </View>
                        <View className='shareVip-box-imgCode'>
                            <Image src={getImg('/group1/M00/00/5D/wKgBZF_KCOGEB9xeAAAAAJTAI2Q115.jpg')} className='shareCode' showMenuByLongpress />
                            <Text className='shareText'>长按保存二维码,添加客服</Text>
                        </View>
                    </View>
                    </View>
                    
                    { isOpen && <View className='sharVip-dialog'>
                        <View className='mask' onClick={this.onDialogCancel}></View>
                        <View className='dialog-wrapper dialog-custom'>
                            <View className='cancel-btn' onClick={this.onDialogCancel}>
                                <IconFont name='common-icon_guanbi' size={22} color='rgb(255,255,255)' />
                            </View>
                            {/* 内容 */}
                            <View className='sharVip-dialog-content'>
                                <Image src={imgUrl} className='shareHaibao' mode='widthFix' />
                                <View className='shareBtn' onClick={this.shareToFriend}>保存图片</View>
                            </View>
                        </View>
                    </View>}
                </View>
            );
        }
    }
    
    export default ShareVip;
    

      

  • 相关阅读:
    年轻人的第一个 Spring Boot 应用,太爽了!
    面试问我 Java 逃逸分析,瞬间被秒杀了。。
    Spring Boot 配置文件 bootstrap vs application 到底有什么区别?
    坑爹的 Java 可变参数,把我整得够惨。。
    6月来了,Java还是第一!
    Eclipse 最常用的 10 组快捷键,个个牛逼!
    Spring Cloud Eureka 自我保护机制实战分析
    今天是 Java 诞生日,Java 24 岁了!
    厉害了,Dubbo 正式毕业!
    Spring Boot 2.1.5 正式发布,1.5.x 即将结束使命!
  • 原文地址:https://www.cnblogs.com/nangras/p/14137359.html
Copyright © 2020-2023  润新知