2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Titanium 学習履歴 画像base64エンコード

Posted at

titaniumのbase64エンコードメソッドはエンコードに失敗するため、
html5のcanvasの機能を使ってbase64させる必要があるらしい

(例)
<getPhoto.js>
//ライブラリから画像データを取得し、canvasにデータのパスを渡す
Titanium.Media.openPhotoGallery({
  success:function(event){
    if(event.mediaType == Ti.Media.MEDIA_TYPE_PHOTO){
      var image = event.media;
      var Settings = Titanium.Filesystem.getFile(Titanium.Filesystem.tempDirectory,'Settings');
      Settings.createDirectory();
      var newFile = Titanium.Filesystem.getFile(Settings.nativePath,'canvas.png');
      newFile.createFile();
      var path = newFile.nativePath;
      Titanium.App.fireEvent("sendImageTocanvas",{
        event:eventName,
        path:path
      });
    }
  },
});

//引数のから画像データをBase64エンコードし、値をFireする
<canvas.html>
Titanium.App.addEventListener('sendImageTocanvas',function(e){
    var img = new Image();
    img.src = e.path;
    img.onload = function (){
      canvas.width = img.naturalWidth;
      canvas.height = img.naturalHeight;
      var context = canvas.getContext('2d');
      context.drawImage(img, 0, 0);
      var imgstr = canvas.toDataURL();
      var base64 = {str:imgstr};
      Ti.App.fireEvent(e.event,base64);
    };
});

//Base64エンコードされた画像データを必要部分を抜き出し、imageViewに適用する
Ti.App.addEventListener('getBase64_PhotoItem',function(e){
  var image = e.str;
  var idx = imagefile.indexOf('base64,');
  var img = imagefile.substring(idx + 'base64,'.length);
  
  //あらかじめ作っていたimageViewに反映する。
  imageView.image = Ti.Utils.base64decode(img);
}

※ご指摘ありましたら、細かな点でもコメントよろしくお願いします。

2
2
0

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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?