FileReader在ios上面的坑(图片转base64)

本文介绍了一种使用FileReader API和Canvas进行图片压缩并转换为Base64格式的方法,适用于网页应用中图片的预览和上传优化。

https://www.cnblogs.com/samsimi/p/6559591.html

var fileUpload = function(obj, callback){

            //检测浏览器是否支持FileReader对象
            if(typeof FileReader == "undefined"){
                alert("您的浏览器不支持FileReader对象!");
            }
            var file = obj;
            //判断类型是不是图片
            if(!/image\/\w+/.test(file.type)){
                alert("请确保文件为图像类型");
                return false;
            }
            var reader = new FileReader();
            reader.onload = function (e) {
                var img = new Image,
                    width = 95,    //图片resize宽度
                    quality = 0.2,  //图像质量
                    canvas = document.createElement('canvas'),
                    drawer = canvas.getContext("2d");
                img.src = this.result;
                var scale = parseInt(img.height / img.width);
                img.width=width;
                img.height = width * scale;
                canvas.width = img.width;
                canvas.height = img.height;
                drawer.drawImage(testimgId, 0, 0, canvas.width, canvas.height);
                var base64 = canvas.toDataURL('image/jpeg', 0.2);
                console.log(base64);
                var image_base64 = img.src.replace("data:image/png;base64,","");
                image_base64=encodeURIComponent(image_base64);
                alert("19")
                // 调用回调
                callback&&callback(img.src);
            }
            reader.readAsDataURL(file);

        }

 

转载于:https://www.cnblogs.com/beimingbingpo/p/9753028.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值