Typescript 如何在ionic 3中将图像转换为字符串数据
目前我正在开发Ionic3项目。 现在我尝试将图像数据从UI存储到sqlite。 但我不确定该怎么做。 如果你已经做了,请帮助我Typescript 如何在ionic 3中将图像转换为字符串数据,typescript,ionic-framework,Typescript,Ionic Framework,目前我正在开发Ionic3项目。 现在我尝试将图像数据从UI存储到sqlite。 但我不确定该怎么做。 如果你已经做了,请帮助我 我该怎么做呢?首先,你需要学习如何拍照,为此你应该参考此链接 之后图像进入图像的路径如下file:///image_path,因此您必须像这样将此路径传递给base64插件 为此,您必须安装插件 $IONAL cordova插件添加com-badrit-base64 $npm安装--save@ionic native/base64 public present
我该怎么做呢?首先,你需要学习如何拍照,为此你应该参考此链接 之后图像进入图像的路径如下
file:///image_path
,因此您必须像这样将此路径传递给base64插件
为此,您必须安装插件
$IONAL cordova插件添加com-badrit-base64
$npm安装--save@ionic native/base64
public presentActionSheet() {
let actionSheet = this.actionSheetCtrl.create({
title: 'Select Image Source',
buttons: [
{
text: 'Load from Library',
handler: () => {
this.takePicture(this.camera.PictureSourceType.PHOTOLIBRARY);
}
},
{
text: 'Use Camera',
handler: () => {
this.takePicture(this.camera.PictureSourceType.CAMERA);
}
},
{
text: 'Cancel',
role: 'cancel'
}
]
});
actionSheet.present();
}
public takePicture(sourceType) {
let resUser: string = "res.users";
// Create options for the Camera Dialog
var options = {
quality: 100,
sourceType: sourceType,
saveToPhotoAlbum: true,
correctOrientation: true
};
// Get the data of an image
this.camera.getPicture(options).then((imagePath) => {
// Special handling for Android library
if (this.platform.is('android') && sourceType === this.camera.PictureSourceType.PHOTOLIBRARY) {
this.base64.encodeFile(imagePath).then((base64String: string) => {
// console.log("-----base64String-----" + base64String);
let imageSrc = base64String.split(",");
console.log("---Splitted image string----" + imageSrc[1]);
this.src = imageSrc[1]
let args = {
image: imageSrc[1]
}
this.odooRpc.updateRecord(resUser, this.data[0].id, args).then((res: any) => {
console.log("----res of updation---" + res);
}).catch((err: any) => {
this.presentToast("There is some problem in uploading image")
})
})
this.filePath.resolveNativePath(imagePath)
.then(filePath => {
let correctPath = filePath.substr(0, filePath.lastIndexOf('/') + 1);
let currentName = imagePath.substring(imagePath.lastIndexOf('/') + 1, imagePath.lastIndexOf('?'));
this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
});
} else {
this.base64.encodeFile(imagePath).then((base64String: string) => {
// console.log("-----base64String-----" + base64String);
let imageSrc = base64String.split(",");
console.log("---Splitted image string----" + imageSrc[1]);
this.src = imageSrc[1]
let args = {
image: imageSrc[1]
}
this.odooRpc.updateRecord(resUser, this.data[0].id, args).then((res: any) => {
console.log("----res of updation---" + res);
}).catch((err: any) => {
this.presentToast("There is some problem in uploading image")
})
})
console.log("-------inside else IMage Path------" + imagePath);
var currentName = imagePath.substr(imagePath.lastIndexOf('/') + 1);
var correctPath = imagePath.substr(0, imagePath.lastIndexOf('/') + 1);
this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
}
}, (err) => {
this.presentToast('Error while selecting image.');
});
}
private createFileName() {
var d = new Date(),
n = d.getTime(),
newFileName = n + ".jpg";
return newFileName;
}
// Copy the image to a local folder
private copyFileToLocalDir(namePath, currentName, newFileName) {
this.file.copyFile(namePath, currentName, cordova.file.dataDirectory, newFileName).then(success => {
this.lastImage = newFileName;
}, error => {
this.presentToast('Error while storing file.');
});
}
参考这封信,谢谢你的帮助。但我做不到。请解释一下你的经历,你能告诉我你是如何得到这个形象的吗?谢谢你的帮助。我试过了。但是你能告诉我imagePath的情况吗?此变量中需要哪个参数?请提供更多帮助。如果您已经参考了我提供给您的链接,请参考更新的答案谢谢。我试着用你的方式做这件事,但没有得到好结果。由于我是初学者,请你从头到尾仔细解释一下好吗?我需要你的全力帮助,请帮助我。谢谢你的帮助。