getFileNaturalSize static method
获取本地文件图片的天然像素尺寸(使用 image_size_getter,命中缓存则直接返回)
filePath 支持普通路径或以 file:// 开头的路径
Implementation
static ui.Size? getFileNaturalSize(String filePath) {
if (filePath.isEmpty) return null;
String path = filePath;
if (path.startsWith('file://')) {
path = path.substring(7);
}
final cached = _filePathSizeCache.remove(path);
if (cached != null) {
_filePathSizeCache[path] = cached;
return cached;
}
try {
final file = File(path);
if (!file.existsSync()) return null;
final result = ImageSizeGetter.getSizeResult(FileInput(file));
final size = _naturalSize(
result.size.width,
result.size.height,
needRotate: result.size.needRotate,
);
if (path.length * 2 + 128 <= _maxFileCacheBytes) {
_filePathSizeCache[path] = size;
var cost = _filePathSizeCache.keys.fold<int>(
0,
(sum, key) => sum + key.length * 2 + 128,
);
while (_filePathSizeCache.length > _maxCacheEntries ||
cost > _maxFileCacheBytes) {
final oldest = _filePathSizeCache.keys.first;
cost -= oldest.length * 2 + 128;
_filePathSizeCache.remove(oldest);
}
}
return size;
} catch (e) {
// TODO(wrapper): RCIMWrapperPlatform 接口待 wrapper 层实现后启用
// RCIMWrapperPlatform.instance.writeLog(
// 'ImageUtil getFileNaturalSize error',
// '',
// 0,
// 'error: $e, path: $filePath');
return null;
}
}