LoginSignup
4
4

More than 5 years have passed since last update.

Android向けに作ったTitaniumアプリをiOS上で動かすためにしたこと

Last updated at Posted at 2014-02-05

間違いありそうだけど、とりあえず今のところ必要だったことを書いておきます。
まだ追加があるので後日更新する予定。

Titanium.Database.ResultSetのfieldCountの参照方法の変更

resultset.js

var length = resultSet.fieldCount; 
if(Ti.Platform.osname == "android"){
        length = resultSet.fieldCount;  
}
else
{
        length = resultSet.fieldCount();        
}

Fileから読み込んだBlobのheightの値の参照方法の変更

こんな感じで読んだ画像ファイルの高さと幅の値を参照するのに

android_blob.js
var blob = Ti.Filesystem.getFile(filepath).read();

アンドロイドは以下のような感じでいけたんですが、iOSだとheightがNaNになってしまって適切な値が読めなかった。

android_blob.js
blob.getWidth();
blob.getHeight();

なので一旦imageview経由したら取得できた。後でもう一度検証してみる予定。

ios_blob.js
var imageView = Titanium.UI.createImageView({
                            image: blob,
                            width: 'auto',
                            height: 'auto'
                    });
var originalWidth = imageView.toImage().width;
var originalHeight = imageView.toImage().height;

ImageViewにファイルパスを渡すとときに渡すファイルパスの形式を変更

android上ではこんな感じでいけたけど、iOSだと画像が表示されなかった。

android_imageview.js
var filePath = "/images/sample.png";
Ti.UI.createImageView( { image: filePath } );

Ti.Filesystem.File経由でnativePathに変換したらiOSでも画像が表示されるようになった。

ios_imageview.js
var filePath = "/images/sample.png";
var nativeFilePath = Ti.Filesystem.getFile(filePath).nativePath;
Ti.UI.createImageView( { image: nativeFilePath } );

ファイルのcopy方法の変更

android上でcopy使ってたら、iOSだとないって怒られた

android_copy.js
var file = Ti.Filesystem.getFile("/images/sample.png");
file.copy(destFilePath);

仕方なく自前でread, write.
これくらい対応してほしい。。。

android_copy.js
var destFile = Ti.Filesystem.getFile(destFilePath);
var srcFile = Ti.Filesystem.getFile("/images/sample.png");
destFile.write(srcFile.read());

画像のリサイズ方法

android上での画像のリサイズにandroid用のモジュールを以下のような感じで使っていたので、
モジュールビルドする際に怒られた。

tiapp.xml
<module version="0.5">org.selfkleptomaniac.ti.imageasresized</module>

このモジュールはandroid限定ですって書かないといけないらしい。

tiapp.xml
<module version="0.5" platform="android">org.selfkleptomaniac.ti.imageasresized</module>

コード上も少し変えたけど、blobのimageAsResizedっていう標準のメソッドに切り替えただけ。

Titanium.Databaseに対するの大きな整数値の保存方法

Androidは以下のコードでresultとtの値が同じになるが、iphoneはresultの値がおかしな値になる.
小さい値だと大丈夫っぽいけど,,,

database.js
var db = Ti.Database.open("inttest");
db.execute("CREATE TABLE foo (bar INTEGER)");
var t = new Date().getTime();
db.execute("INSERT INTO foo VALUES (?)", t);
var rs = db.execute("SELECT bar FROM foo");
var result = rs.field(0);
rs.close();
db.close();
alert("written value=" + t + '\nread value=' + result);

対処方法は以下のようにTEXTとして保存してparseIntを使って復元する。
これはすごいバグですね。しかも3年前からある問題っぽいけど。。。。
http://developer.appcelerator.com/question/120826/sqlite-integer-storage-issue-on-iphone

var db = Ti.Database.open('inttest');
db.execute("CREATE TABLE foo (bar TEXT)");
var t = new Date().getTime();
db.execute("INSERT INTO foo VALUES (?)", t);
var rs = db.execute('SELECT bar FROM foo');
var result = rs.field(0);
rs.close();
db.close();
alert("written value=" + t + '\nread value=' + parseInt(result));
4
4
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4