我有一个很奇怪的问题。我有一个手表应用程序,正在尝试以编程方式显示图像。调试代码时,我可以看到图像已正确加载,但是在模拟器或真实设备上却看不到图像。我已经在监视和扩展捆绑资源中添加了该图像。
首先,我使用下面的代码打开视图。
NSArray *objects =[NSArray arrayWithObjects:@"MainPageView",@"InterfaceController",@"PoseImageView",nil];
[self presentControllerWithNames:objects contexts:nil];
在PoseImageView
内部,我试图显示具有以下内容的图像:
UIImage *imgTest = [UIImage imageNamed:[NSString stringWithFormat:@"1.jpg", strAngle]]; // I can see 1.jpg in debugger
[_imgPose setImage: imgTest];
手表或模拟器上没有图像。我想知道如何解决此问题。
注意:从资产中也无法加载。
注2:仅当视图为初始视图时,我才能显示图像。
答案 0 :(得分:0)
您正在尝试将imageNamed用于.jpg,它将不起作用。 尝试使用
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Path Provider',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Path Provider', storage: FileStorage(),),
);
}
}
class FileStorage {
Future<String> get _localPath async {
final directory = await getTemporaryDirectory();
return directory.path;
}
Future<File> get _localFile async {
final path = await _localPath;
return File('$path/file.txt');
}
void readFile() {
/* What to do? */
}
Future<Null> writeFile(String text) async {
final file = await _localFile;
IOSink sink = file.openWrite(mode: FileMode.append);
sink.add(utf8.encode('$text'));
await sink.flush();
await sink.close();
}
}
class MyHomePage extends StatefulWidget {
final FileStorage storage;
MyHomePage({Key key, this.title, this.storage}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final myController = TextEditingController();
@override
void dispose() {
// TODO: implement dispose
myController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
// TODO: implement build
return new Scaffold(
appBar: new AppBar(
title: new Text('Testing'),
),
body: new Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: new TextField(
controller: myController,
decoration: new InputDecoration(
hintText: 'Enter the text',
),
),
),
// StreamBuilder(
// stream: widget.storage.readCounter().asStream(),
// )
],
),
floatingActionButton: new FloatingActionButton(
child: new Icon(Icons.save_alt),
onPressed: () {
widget.storage.writeFile(myController.text);
},
),
);
}
}