2016-08-02 77 views
1

如何从nativescript相机模块获取照片src?如何访问图像src nativescript

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

如何获得图像的src?

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

+0

对不起。我弄乱了我的编辑。我刚刚提交了一个新的修改来修复它。对此很抱歉。下次请删除这些行号和其他东西,我们将不需要编辑您的代码:) –

回答

4

要上传到火力点,你需要通过它的路径上传的图片:

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

});

+0

fromNativeSource(data)中的data是什么? ? – IvRRimUm

+0

'data'参数是本地图像对象。对于Android,它是位图和iOS - UIImage –

+0

不幸的是,我似乎无法得到它的工作。我有图像对象,我不明白如何从对象中获取路径。 – IvRRimUm

2

想通了,这个工程:

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