[size=large][b]数据的保存[/b][/size]

这篇我们来说说在应用中使用的数据,在程序终了后也能保存的方法。具体就是在我们的Twitter客户端应用中,当要发送的Tweet处于编辑中的时候,即使应用终了后,编辑中的内容还能够在下次再编辑。iPhone应用中,由于用户的不经意的操作,内存的自动释放等等原因,在无意识中程序就被终了后,未保存的数据也将消失。所以保存编辑中的数据是很重要的。

首先,保存输入的Tweet的代码如下(还是在我们以前的message_window.js中追加代码):

win.addEventListener(
    'close',
    function(){
        var text = textArea.value;
        Ti.App.Properties.setString('previousText', text);
    }
);

win.addEventListener(
    'open',
    function(){
        var text = Ti.App.Properties.getString('previousText');
        if ( text ) {
            textArea.value = text;
        }
    }
);



在window关闭的时候,window中的textarea等UI控件输入的内容将会消失,所以我们在window的close事件中追加监听事件,将输入的数据进行保存。



这里我们使用的方法是Titanium.App.Properties。



Titanium.App.Properties是用来管理键值对数据的一个很方便的对象。在保存数据的时候,在Ti.App.Properties.setString相对应的Key的值中设置你要保存的值即可。相反,取值的时候,通过Ti.App.Properties.getString方法,取得你想取的Key的值。setString和getString是设置字符串的方法,类似的还有其他数据类型的方法。特别要说的是,要想原封不动的保存JavaScript对象是不可以的,你需要先通过JSON等处理,将对象序列化后再保存。



关于String以外的值,这里我们通过保存位置信息来说说:



win.addEventListener(
    'close',
    function(){
        var text = textArea.value;
        Ti.App.Properties.setString('previousText', text);

        if (mapview.visible) {
            var loc = mapview.location;
            var locationList =
                [loc.latitude,loc.longitude,loc.latitudeDelta,loc.longitudeDelta];
            Ti.App.Properties.setList('locationList', locationList);
        }
    }
);

win.addEventListener(
    'open',
    function(){
        var text = Ti.App.Properties.getString('previousText');
        if ( text ) {
            textArea.value = text;
        }
        if ( Ti.App.Properties.hasProperty('locationList') ) {
            var locationList = Ti.App.Properties.getList('locationList');
            mapview.show();
            mapview.setLocation(
                {
                    latitude : locationList[0],
                    longitude : locationList[1],
                    latitudeDelta : locationList[2],
                    longitudeDelta : locationList[3]
                }
            );
        }
    }
);



在这个例子中,mapview中要设置的4个关于位置的信息值,能够将其变化成数据后保存起来。原封不动保存对象是不可以的,但是如果有很多值的话,你可以其以数组的形式保存。



当我们要判断某个Key中是否有值的时候,可以使用Ti.App.Properties.hasProperty。



[size=large][b]文件的保存[/b][/size]



在上边的例子中,我们通过使用Ti.App.Properties来保存了数据。在Titanium中也能够很方便的读写文件。以下通过例子来说明如何将数据保存到文件中:


var fileName = 'tweet.txt';
win.addEventListener(
    'close',
    function(){
        var text = textArea.value;
        var loc;
        if (mapview.visible) {
            loc = mapview.location;
        }
        var json = JSON.stringify({previousText:text,location:loc});
        var file  = Ti.Filesystem.getFile(
            Titanium.Filesystem.resourcesDirectory + '/' + fileName
        );
        file.write(json);
    }
);

win.addEventListener(
    'open',
    function(){
        var file  = Ti.Filesystem.getFile(
            Titanium.Filesystem.resourcesDirectory + '/' + fileName
        );
        var json = file.read();
        if ( !json || json.length <= 0) {
            return;
        }
        var data = JSON.parse(json);
        var text = data.previousText;
        var location = data.location;
        if ( text ) {
            textArea.value = text;
        }
        if ( location ) {
            var locationList = Ti.App.Properties.getList('locationList');
            mapview.show();
            mapview.setLocation(location);
        }
    }
);



在写文件的时候需要注意的一点是:获取文件句柄的方法是Titanium.Filesystem.getFile。


其中的参数就是文件的路径。


Titanium.Filesystem.resourcesDirectory + '/' + fileName



这里我们通过resourcesDirectory来指定文件路径,相应的其他的可读写路径,Titanium也提供了很多种,大家可以查查API。



在往文件中写入对象的数据的话也一样要通过JSON来序列化。



Titanium中已经为我们提供了JSON的序列化(stringify)和反序列化(parse)方法。上边的例子中大家可以看到具体的用法。



[size=large][b]图像的保存[/b][/size]



在Tweet的编辑页面中,我们还表示了一个图像,到现在为止,我们都是通过从相册中选择出来的图片,所以图片本身就保存在相册中,但是当我们通过相机拍照的话,就需要将图片保存在相册中。


Titanium.Media.saveToPhotoGallery(image)



通过以上代码就能将image图片保存到相册中。



var file  = Ti.Filesystem.getFile(
    Titanium.Filesystem.resourcesDirectory + '/' + fileName
);
file.write(image)



当然也可以通过这样的方法,将图像以文件的形式保存。