使用expo-image在Web端遇blurhash长度错误,移动端正常求解决方案
解决 Expo Image 在 Web 端出现
The blurhash string must be at least 6 characters 错误的方案 原因1:Web 端对 blurhash 的校验逻辑更严格
Expo Image 的 Web 实现与移动端存在差异,移动端会自动兼容无效的短 blurhash,但 Web 端会直接抛出校验错误。如果你的 blurhash 是动态获取的,大概率存在空字符串、长度不足6的情况,移动端兼容了这类场景但 Web 端没有。解决方法1:提前校验 blurhash 长度
在传入 blurhash 前先做判断,仅当长度≥6时才传入,否则不传或使用默认占位 blurhash。示例代码:const validBlurhash = blurhash?.length >= 6 ? blurhash : null; <Image source={{ uri: imageUrl }} blurhash={validBlurhash} style={{ width: 200, height: 200 }} />原因2:blurhash 字符串包含隐藏字符
有些 blurhash 看起来长度达标,但实际包含空格、换行等隐藏字符,导致有效长度不足。Web 端校验会计算整个字符串长度,而移动端可能自动过滤这类无效字符。解决方法2:清理 blurhash 中的无效字符
传入前先对 blurhash 做 trim 处理,过滤非必要字符:const cleanedBlurhash = blurhash?.trim() || null; const validBlurhash = cleanedBlurhash?.length >= 6 ? cleanedBlurhash : null; <Image source={{ uri: imageUrl }} blurhash={validBlurhash} style={{ width: 200, height: 200 }} />原因3:Expo Image 版本存在兼容性bug
部分旧版本的 Expo Image 在 Web 端的 blurhash 校验逻辑有问题,升级到最新稳定版可解决。执行以下命令升级:npx expo install expo-image@latest
内容的提问来源于stack exchange,提问作者Neville Lemein
相关产品推荐
相关产品推荐

