2017-12-15 4 views
0

イメージピッカープラグインを使用しています。画像ギャラリーを開き、1つまたは複数の画像を選択できます。イメージピッカー関数内のNativescriptバインド値

私の問題は、イメージのパスをxmlイメージsrcにバインドする方法です。 getImage()関数内では機能しません。

XML:

<Image class="imageSource" src="{{ thumb }}" stretch="none" /> 

typescriptです:

import { Observable } from 'data/observable'; 
import * as imagepicker from "nativescript-imagepicker"; 

var counter = 0; 
var fs = require('file-system'); 

export class AssistenceViewModel extends Observable { 

    thumb:any; 

public addImage(){ 
    dialogs.action({ 
     message: "Opções", 
     cancelButtonText: "Cancelar", 
     actions: ["Câmera", "Galeria"] 
    }).then(result => { 

     console.log("Dialog result: " + result); 
     if(result == "Câmera"){ 

      //Do action1 
      console.log("Abrir camera"); 

     }else if(result == "Galeria"){ 

      console.log("Abrir galeria"); 
      let context = imagepicker.create({ 
       mode: "single" 
      }); 

      context.authorize().then(function() { 
       return context.present(); 
      }).then(function(selection) { 

       selection.forEach(function(selected){ 

        selected.getImage().then(function(imagesource){ 

         var localPath = null; 

         if(platformModule.device.os === "Android"){ 
          localPath = selected.android; 
          console.log("localPath android: " +localPath); 
         }else { 
          // selected_item.ios for iOS is PHAsset and not path - so we are creating own path 
          let folder = fs.knownFolders.documents(); 
          let path = fs.path.join(folder.path, "Test" + counter + ".png"); 
          let saved = imagesource.saveToFile(path, "png"); 

          localPath = path; 
          console.log("localPath iOS: " +localPath); 
         } 

         if(localPath){ 
          this.thumb = localPath // this is not working 
          console.log("thumb: "+this.thumb); // this is not working 
         } 

        }); 

       });     

      }).catch(function(e) { 
       console.log(e); 
      }); 
     } 
    }); 
    } 
} 

にconsole.logに対する結果( "localPathにアンドロイド:" +でlocalPath)。

localPath android: /storage/emulated/0/DCIM/Camera/IMG_20171213_224917038.jpg 

が、私はthis.thumbのためのすべてのログを取得することはできません。

答えて

0

あなたはどちらか"cache the meaning of this"

例えば、こののコンテキストを維持するためにTS arrow functionsを使用する必要があります矢印機能 //より多くのコードこの行の上

.then(() => { 
       return context.present(); 
      }).then((selection) => { 

       selection.forEach((selected) => { 

        selected.getImage().then((imagesource) => { 

それともthat = this;パターン

var that = this; 
// ... your code in the promises follows 
that.thumb = newValue; 
+0

のために、私はこのビデオhttps://youtu.be/uI5cZTC12sUそのfollowiingを行っているが、画像はまだありませんでしたxmlビューで表示します。 –

関連する問題