编辑文件时我遇到了一个小问题。
我使用下面的代码在ios上保存具有特定名称的文件(图像)。 但问题是当我替换存储在文件中的图像(例如temp.jpg)时,应用程序仍会在我打开文件时拾取上一个图像。但是,可以在资源管理器中看到新图像。 如果我重新启动应用程序,则在打开图像时会出现新图像。
var folder = Ti.Filesystem.getFile(Ti.Filesystem.externalStorageDirectory, 'DocImages');
// DocImages is a folder for files
// ImageVar contains the blob for the new image from camera or gallery
// docImgModel is the model class containing the name and image path of the image
if(imageVar !== null && imageVar !== undefined){
if (docImgModel.imagePath !== null && docImgModel.imagePath !== undefined){
tempFile = Ti.Filesystem.getFile(docImgModel.imagePath);
if (tempFile.exists()) {
tempFile.deleteFile(); // deleting already existing file
}}
// in case of changing the image stored in a file(the case in which i have a
// problem) the imgFile(below) is same as docImgModel.imagePath (above)
imgFile = Ti.Filesystem.getFile(Ti.Filesystem.externalStorageDirectory + 'DocImages/', filenameWithExtension); // creating a new file
// writing image to file
imgFile.write(imageVar);
Ti.API.info('new image saved');
}
}
我想知道钛是否保存了已经打开的文件的缓存,因此无法显示新图像。 如果没有,是否还有其他我做错的事情,或者我能做些什么来使它发挥作用。
我只想显示新保存的图像。有没有办法做到这一点。
感谢。
答案 0 :(得分:2)
我没有使用从设备打开文件,但在尝试更新屏幕上的数据时遇到了类似的问题。如果您在打开应用程序时说它加载了正确的图像,那么用于加载图像的代码看起来正确且有效。我假设这是你上面发布的代码。即使这似乎是更完整文件的代码片段。
您没有发布任何UI代码,这可能是您真正的问题所在。你有一个对象,我猜的某种视图,在加载新图像之前已经使用旧图像渲染了。因此,调试时,您可能会在上面的代码中看到新图像的数据已加载,但UI元素尚未正确分配或更新。
作为一项测试,我建议您在应用程序中添加一些测试代码,以便销毁UI元素并重新创建它们,在这种情况下,您可能会看到图片正确显示。
根据这篇文章:http://developer.appcelerator.com/question/31181/simple-image-refresh
只需将加载的图像指定给图像的网址即可更新。您的示例代码不会显示您尝试更新的图像对象以及如何通过加载图像的代码进行通信。
// this sets the background color of the master UIView (when there are no windows/tab groups on it)
Titanium.UI.setBackgroundColor('#000');
// create tab group
var tabGroup = Titanium.UI.createTabGroup();
var image1 = 'image1.png';
var image2 = 'image2.png';
//
// create base UI tab and root window
//
var win1 = Titanium.UI.createWindow({
title:'Tab 1',
backgroundColor:'#fff'
});
var tab1 = Titanium.UI.createTab({
icon:'KS_nav_views.png',
title:'Tab 1',
window:win1
});
var img = Titanium.UI.createImageView({
width:100,
height:100,
top:50,
left:110,
url:image1
});
win1.add(img);
var btn = Titanium.UI.createButton({
title:'load',
width:100,
height:35,
top:50,
left:0
});
function switchImage(){
if(img.url == image1){
img.url = image2;
} else {
img.url = image1;
}
}
btn.addEventListener('click',function(){
switchImage();
});
setInterval(switchImage,3000);
win1.add(btn);
//
// add tabs
//
tabGroup.addTab(tab1);
// open tab group
tabGroup.open();