Help us understand the problem. What is going on with this article?

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

More than 5 years have passed since last update.

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);
}

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

ytakao
モバイルエンジニアです。 モバイルアプリの設計、インフラ回りを特に学習中です
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした