gpt4 book ai didi

Flutter 在调试与 Release模式下的不同行为

转载 作者:IT王子 更新时间:2023-10-29 06:40:34 32 4
gpt4 key购买 nike

我正在为 flutter 应用程序开发这些功能:从图库加载图像、调整大小并保存。

调整大小部分是 CPU 密集型操作,所以我遵循了建议的方法 here使用 isolate 获得更好的用户体验。

当我在 Debug模式下运行代码时没有问题,但是当我在 Release模式下尝试相同的代码时,我的图像保存在 wrong way 中。 .

有人可以帮我理解我解释的问题吗?

我只在这个设备上测试代码:HUAWEI P9 lite VNS-L31 with android 7.0

这里是重现问题的代码。这是一个新的 flutter 项目,包含以下文件:

pubspec.yaml 中我添加了这个部分:

dependencies:
path_provider: ^0.4.1
image_picker: ^0.4.10
image: ^2.0.4
flutter:
sdk: flutter

所有代码都在lib/main.dart中:

import 'dart:async';
import 'dart:io';
import 'dart:isolate';

import 'package:flutter/material.dart';
import 'package:image/image.dart';
import 'package:image_picker/image_picker.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart' as p;

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new MyHomePage(),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);

@override
_MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
File _file;

Future<File> _getImage() async {
File image = await ImagePicker.pickImage(source: ImageSource.gallery);
if (image != null) {
return image;
}
return null;
}

static decode(DecodeParam param) async {
var p = await param.file.readAsBytes();
var image = decodeImage(p);
var thumbnail = copyResize(image, 120);
param.sendPort.send(thumbnail);
}

void _displayImage() async {
setState(() {
_file = null;
});

File file = await _getImage();
ReceivePort receivePort = new ReceivePort();

await Isolate.spawn(decode, new DecodeParam(file, receivePort.sendPort));

var image = await receivePort.first;

Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path;

File profilePictureFile =
File(p.join(tempPath, 'thumbnail' + _counter.toString() + '.png'))
..writeAsBytesSync(encodePng(image));

setState(() {
_counter++;
_file = profilePictureFile;
});
}

@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
_file != null
? Container(
height: 200.0,
width: 200.0,
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
fit: BoxFit.fitWidth, image: FileImage(_file))))
: Container(),
],
),
),
floatingActionButton: new FloatingActionButton(
onPressed: _displayImage,
child: new Icon(Icons.add),
),
);
}
}

class DecodeParam {
final File file;
final SendPort sendPort;
DecodeParam(this.file, this.sendPort);
}

flutter doctor -v:

[√] Flutter (Channel master, v0.9.7-pre.61, on Microsoft Windows [Versione 10.0.15063], locale it-IT)
• Flutter version 0.9.7-pre.61 at C:\src\flutter
• Framework revision 2d81adf74c (2 days ago), 2018-10-05 22:29:37 -0700
• Engine revision 572fa5646a
• Dart version 2.1.0-dev.6.0.flutter-c6254163dc

[√] Android toolchain - develop for Android devices (Android SDK 28.0.1)
• Android SDK at d:\Profiles\alarosa\AppData\Local\Android\sdk
• Android NDK location not configured (optional; useful for native profiling support)
• Platform android-28, build-tools 28.0.1
• Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
• Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b02)
• All Android licenses accepted.

[√] Android Studio (version 3.1)
• Android Studio at C:\Program Files\Android\Android Studio
• Flutter plugin version 26.0.1
• Dart plugin version 173.4700
• Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b02)

[!] IntelliJ IDEA Community Edition (version 2018.1)
• IntelliJ at C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.1
X Flutter plugin not installed; this adds Flutter specific functionality.
X Dart plugin not installed; this adds Dart specific functionality.
• For information about installing plugins, see
https://flutter.io/intellij-setup/#installing-the-plugins

[√] VS Code (version 1.27.2)
• VS Code at d:\Profiles\alarosa\AppData\Local\Programs\Microsoft VS Code
• Flutter extension version 2.19.0

[√] VS Code, 64-bit edition (version 1.27.2)
• VS Code at C:\Program Files\Microsoft VS Code
• Flutter extension version 2.19.0

[√] Connected device (1 available)
• HUAWEI VNS L31 • 4TE0216A14001341 • android-arm64 • Android 7.0 (API 24)

! Doctor found issues in 1 category.

最佳答案

按照@GunterZochbauer 的建议我打开了an issue在 flutter 存储库上。

Flutter 团队正在努力解决这个问题,正如您在 github 线程上看到的那样,Jason-Simmons 建议采取以下解决方法。

将发件人更改为:

param.sendPort.send([thumbnail.width, thumbnail.height, thumbnail.data]);

接收方:

List<dynamic> imageData = await receivePort.first;
var image = new Image.fromBytes(imageData[0], imageData[1], imageData[2]);

关于Flutter 在调试与 Release模式下的不同行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52678023/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com