在小程序开发过程中可能会出现“微信开发者可以正常显示图片,而用iPhone预览或真机调试的时候却没有显示图片”的问题,有可能是因为图片格式iOS不支持所导致,将其图片格式转为.jpg格式即可。
解决方法:
- 判断系统是否为iOS
- 是:可用正则替换replace()的方法将.webp格式图片转换为.jpg格式
- 否:不需要进行处理
// htmlStr为获取到的图片数据,.webp格式
wx.getSystemInfo({
success: (res) => {
// console.log(res);
// console.log(res.system.startsWith("iOS"));
// iOS平台不支持 .webp 图片格式,需要替换成 .jpg
// 判断是否为iOS
if (res.system.startsWith("iOS")) {
// 如果是iOS则需要转换成.jpg格式
// g 是 global 缩写,表示全局替换
const iosStr = htmlStr.replace(/\.webp/g, ".jpg");
// console.log(iosStr);
this.goods_introduce = iosStr;
} else {
// 其它平台不需要处理
this.goods_introduce = htmlStr;
}
},
});