2016-08-02 73 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()]); 
     }); 
    }); 
    }