我正在使用此插件:https://github.com/blinkmobile/cordova-plugin-sketch与Ionic 3。
我的最后一个重要步骤是将结果放在回调函数之外,以便进一步使用它。
这是我的代码:
anhangArray: Array<{ name: string, value: string }>=[];
takePhoto() {
const options: CameraOptions = {
quality: 100,
destinationType: this.camera.DestinationType.FILE_URI,
encodingType: this.camera.EncodingType.JPEG,
mediaType: this.camera.MediaType.PICTURE,
correctOrientation: true,
allowEdit: false,
}
this.camera.getPicture(options).then((imageData) => {
// imageData is either a base64 encoded string or a file URI
// If it's base64:
//base64Image = 'data:image/jpeg;base64,' + imageData;
//>>> FILE_URI
this.getSketch(imageData);
}, (err) => {
// Handle error
});
}
getSketch(src: string) {
window.navigator.sketch.getSketch(this.onSuccess, this.onFail, {
destinationType: window.navigator.sketch.DestinationType.DATA_URL,
encodingType: window.navigator.sketch.EncodingType.JPEG,
inputType: window.navigator.sketch.InputType.FILE_URI,
inputData: src
});
}
onSuccess(imageData) {
var _mythis = this;
if (imageData == null) { return; }
// do your thing here!
setTimeout(function () {
if (imageData.indexOf("data:image") >= 0) {
} else {
imageData = "data:image/jpeg;base64," + imageData;
}
_mythis.anhangArray.push({ name: "anhang_" + parseInt(_mythis.user._kunnr), value: imageData });
console.log(this.anhang);
}, 0);
}
错误:未捕获的TypeError:无法读取未定义的属性'anhangArray'
答案 0 :(得分:2)
您的问题在于 window.navigator.sketch.getSketch()。当 window.navigator.sketch.getSketch 触发您的回调 this.onSuccess 时,会更改回调的范围。要解决此问题,您可以更改下面的实现。
getSketch(src: string) {
window.navigator.sketch.getSketch(imageData => {
this.onSuccess(imageData);
}, error => {
this.onFail(error);
}, {
destinationType: window.navigator.sketch.DestinationType.DATA_URL,
encodingType: window.navigator.sketch.EncodingType.JPEG,
inputType: window.navigator.sketch.InputType.FILE_URI,
inputData: src
});
}
答案 1 :(得分:1)
你也可以这样解决:
this.camera.getPicture(options).then((imageData) => {
// imageData is either a base64 encoded string or a file URI
// If it's base64:
//base64Image = 'data:image/jpeg;base64,' + imageData;
//>>> FILE_URI
this.getSketch(imageData).bind(this);
&#13;
其中.bind(this)
将范围传递给您的来电者。