Skip to content

[Bug report] file.length() returns incorrect image size on iOS #1359

@lm83680

Description

@lm83680

Version

3.7.1

Platforms

iOS

Device Model

iPhone13 (ios 18)

flutter info

[✓] Flutter (Channel stable, 3.29.3, on macOS 15.7.2 24G325 darwin-arm64, locale zh-Hans-CN) [1,349ms]
    • Flutter version 3.29.3 on channel stable at /Users/leokent/develop/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision ea121f8859 (8 months ago), 2025-04-11 19:10:07 +0000
    • Engine revision cf56914b32
    • Dart version 3.7.2
    • DevTools version 2.42.3

[✓] Android toolchain - develop for Android devices (Android SDK version 36.1.0) [7.2s]
    • Android SDK at /Users/leokent/Library/Android/sdk
    • Platform android-36, build-tools 36.1.0
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
      This is the JDK bundled with the latest Android Studio installation on this machine.
      To manually set the JDK path, use: `flutter config --jdk-dir="path/to/jdk"`.
    • Java version OpenJDK Runtime Environment (build 21.0.8+-14196175-b1038.72)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 26.1.1) [2.0s]
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 17B100
    • CocoaPods version 1.16.2

[✓] Chrome - develop for the web [11ms]
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2025.2) [11ms]
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 21.0.8+-14196175-b1038.72)

[✓] VS Code (version 1.107.0) [10ms]
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.124.0

[✓] Connected device (4 available) [6.0s]
    • Leo iPhone 13 EM (mobile)       • 00008110-000544913C8A201E • ios            • iOS 18.7.2 22H124
    • macOS (desktop)                 • macos                     • darwin-arm64   • macOS 15.7.2 24G325 darwin-arm64
    • Mac Designed for iPad (desktop) • mac-designed-for-ipad     • darwin         • macOS 15.7.2 24G325 darwin-arm64
    • Chrome (web)                    • chrome                    • web-javascript • Google Chrome 143.0.7499.110

[✓] Network resources [2.5s]
    • All expected network resources are available.

• No issues found!

How to reproduce?

...// Ignore;读取用例图片
final int size = await file.length();
print(size);  // 8547919

如果我使用官包image_picker读取,它将正确获得大小

XFile? x = await ImagePicker().pickImage(source: ImageSource.gallery);
print((await x?.length()));  // 3458909

可复现的图像,ios 相册显示 [3240 x 2160 3.5MB]
Image

Logs

使用 Example code 将得到打印一下内容

flutter: PhotoManager Image Path: /private/var/mobile/Containers/Data/Application/F962640C-6440-4DAF-8215-A7DE7E6E91B8/tmp/flutter-images/43ef903f8dfa82b4aef16f08770867a2_exif.jpg
flutter: PhotoManager Image Size: 8547919
flutter: ImagePicker Image Path: /private/var/mobile/Containers/Data/Application/F962640C-6440-4DAF-8215-A7DE7E6E91B8/tmp/image_picker_C9B9F54D-9AAA-4DED-9B25-7240BB041212-9624-00000496E2AA7A73.jpg
flutter: ImagePicker Image Size: 3458909

Example code (optional)

/// 实际上并不需要UI,但为了直观看出是同一张图片,可以用该Example
class ImagePickerPage extends StatefulWidget {
  @override
  _ImagePickerPageState createState() => _ImagePickerPageState();
}

class _ImagePickerPageState extends State<ImagePickerPage> {
  File? _photoManagerImage;
  File? _imagePickerImage;

  // 异步加载图片
  Future<void> getImage() async {
    // 使用 photo_manager 获取相册图片
    final List<AssetEntity> entities = await PhotoManager.getAssetListPaged(page: 0, pageCount: 1);
    final File? photoManagerFile = await entities.first.file;
    print("PhotoManager Image Path: ${photoManagerFile?.path}");
    print("PhotoManager Image Size: ${await photoManagerFile?.length()}");

    // 使用 image_picker 获取相册图片
    XFile? xFile = await ImagePicker().pickImage(source: ImageSource.gallery);
    final File imagePickerFile = File(xFile?.path ?? '');
    print("ImagePicker Image Path: ${imagePickerFile.path}");
    print("ImagePicker Image Size: ${await imagePickerFile.length()}");

    // 更新状态,渲染图片
    setState(() {
      _photoManagerImage = photoManagerFile;
      _imagePickerImage = imagePickerFile;
    });
  }

  @override
  void initState() {
    super.initState();
    // 在页面加载时获取图片
    getImage();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("异步加载图片"),
      ),
      body: Center(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            // 左侧显示 PhotoManager 图片
            if (_photoManagerImage != null)
              Image.file(
                _photoManagerImage!,
                width: 150,
                height: 150,
              ),
            SizedBox(width: 20), // 两个图片之间的间距
            // 右侧显示 ImagePicker 图片
            if (_imagePickerImage != null)
              Image.file(
                _imagePickerImage!,
                width: 150,
                height: 150,
              ),
          ],
        ),
      ),
    );
  }
}

Contact

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions