isImageUrl static method
判断图片地址是否为有效的图片
Implementation
static Future<bool> isImageUrl(String imageUrl) async {
try {
// 使用 Dio 下载图片头部数据
final dio = Dio();
final response = await dio.get(
imageUrl,
options: Options(
responseType: ResponseType.bytes,
followRedirects: false,
validateStatus: (status) => status! < 400,
),
);
// 检查下载的数据是否为有效的图片
final bytes = response.data as Uint8List;
return isImageFromBytes(bytes);
} catch (e) {
// 处理异常情况
debugPrint('Error checking image URL: $e');
return false;
}
}