1***@qq.com
1***@qq.com
  • 发布:2019-08-09 11:48
  • 更新:2023-03-21 14:34
  • 阅读:8321

uni-app在脚本中不支持Image对象?

分类:uni-app

在图片显示的时候,通过自定义组件来判断资源是否加载出现问题,当在使用脚本的时候,在Hbuilderx的预览窗口,没有问题,当在手机上时,提示的 Image对象 ```javascript
Unhandled promise rejection, ReferenceError: Can't find variable: Image

这是什么问题,如下是部分源代码

const imgIsError = function(url) {
return new Promise(resolve => {
var img = new Image();
console.log(img);
img.onload = function() {
if (this.complete == true) {
resolve(true);
img = null;
}
};
img.onerror = function() {
resolve(false);
img = null;
};
img.src = url;
});
};


2019-08-09 11:48 负责人:无 分享
已邀请:
zmm2113V

zmm2113V - 热爱前端

确实有这个问题new image() app获取不到对象

  • 1***@qq.com

    uni.getImageInfo

    2019-11-22 14:07

  • 叶耳朵

    回复 1***@qq.com: 没啥用啊 你觉得是加载完了 是你图片太小了。图片太大一样的

    2020-02-10 21:10

1***@qq.com

1***@qq.com

我也碰到了同样的问题

const _imgWH = src => {  
                return new Promise((res, rej) => {  
                    let img = new Image();  
                    img.src = src;  
                    img.onload = () => {  
                        res({ width: img.width, height: img.height });  
                    };  
                });  
            };

可以用 uni.getImageInfo 代替

1***@qq.com

1***@qq.com - 放逐的帝王

uniapp 不支持new Image(); 获取不到对象,onload 也不起作用,有没有其他方法啊?

4***@qq.com

4***@qq.com

return new Promise((resolve,reject) => {

                // #ifndef H5  
                    uni.getImageInfo({  
                        src: url,  
                        success: function (image) {  
                            resolve(true);  
                        },  
                        fail:function(err){  
                            resolve(false);  
                        }  
                    });  
                // #endif  

                // #ifdef H5  
                    var img = new Image();  
                    img.onload = function () {  
                        if (this.complete == true){  
                            resolve(true);  
                            img = null;  
                        }else{  
                            resolve(false);  
                        }  
                    }  
                    img.onerror = function () {  
                        resolve(false);  
                        img = null;  
                    }  
                    img.src = url;  
                // #endif  

            })
1***@qq.com

1***@qq.com

问题是失效之后,uniapp中使用echarts,自定义热词云图的形状就没法自定义了,有什么解决办法没?

  • 冯一天

    大佬怎么解决了,同问

    2023-06-20 14:57

  • 1***@qq.com

    回复 冯一天: 啊,就限制了一下大小,没有自定义。

    2023-08-10 10:53

要回复问题请先登录注册