如何访问图像 src nativescript

How to access image src nativescript

如何从 nativescript 相机模块获取照片 src?

public takePicture() {
  cameraModule.takePicture().then(function(picture) {
      console.log("Result is an image source instance");
      var image = new imageModule.Image();
      image.imageSource = picture;
      console.dir(picture);
  });
}

console.dir 输出:

=== dump(): dumping members ===
{
    "android": {
        "constructor": "constructor()function () { [native code] }"
    }
}
=== dump(): dumping function and properties names ===
loadFromResource()
fromResource()
loadFromFile()
fromFile()
loadFromData()
fromData()
loadFromBase64()
fromBase64()
setNativeSource()
saveToFile()
height: 480
width: 640
=== dump(): finished ===

如何获取图片源?

我想上传到firebase,所以我需要src。

要上传到 firebase,您需要通过其路径上传图片:

let imgsrc = this.imageSource.fromNativeSource(data);
let path = this.utils.documentsPath(randomName);
imgsrc.saveToFile(path, this.enums.ImageFormat.png);

this.firebase.uploadFile(path).then((uploadedFile: any) => {
   this.appSettings.setString("fileName", uploadedFile.name);         
      this.router.navigate(['/soundcloud']);
      this.LoadingIndicator.hide();          
}, (error: any) => {
   alert("File upload error: " + error);
 });
}, (err: any) => {
   alert(err);

});

想通了,这行得通:

  public takePicture() {
    cameraModule.takePicture().then((picture) => {
        var image = new imageModule.Image();
        image.imageSource = picture;


        let savePath = fs.knownFolders.documents().path;
        let fileName = 'img_' + new Date().getTime() + '_' + this.currentUserId.getValue() + '.' + enumsModule.ImageFormat.jpeg;
        let filePath = fs.path.join( savePath, fileName );
        picture.saveToFile(filePath, enumsModule.ImageFormat.jpeg);

        this.photoService.uploadImage(filePath, fileName).then((data) => {
          this._router.navigate(["/upload", fileName, this.currentUserId.getValue()]);
        }); 
    }); 
  }