DATA_URL将图像作为数据流返回。现代的高分辨率相机只是因为返回到程序的字符串值的大小而使JavaScript引擎过载。从我的Apache Cordova API食谱:
当我拍照时,我开始连续收到内存警告。我使用的是Cordova 3.5和带有这些设置的相机插件。
var _config;
var pictureSource;
var destinationType; // sets the format of returned value.
var encodingType; // enconding type 0=JPG 1=PNG
/**
* Initialize camera plugin.
* @param {object} config - settings.
*/
function initialize(config) {
alert("CAMERA is comming!!");
// Wait for Cordova to connect with the device
document.addEventListener('deviceready', onDeviceReady, false);
}
/**
* Cordova is ready to be used!
* @param {object} config - settings.
*/
function onDeviceReady() {
console.log("CAMERA is READY!!");
pictureSource=navigator.camera.PictureSourceType;
destinationType=navigator.camera.DestinationType;
encodingType = navigator.camera.EncodingType;
capturePhoto();
}
/**
* Set camera plugin settings.
* @param {object} config - settings.
*/
function setConfig(config) {
_config = config;
}
/**
* Take picture using device camera and retrieve image as base64-encoded string.
*/
function capturePhoto() {
setConfig({ quality: 20, destinationType: destinationType.DATA_URL, encodingType: 0});
navigator.camera.getPicture(onPhotoDataSuccess, onFail, _config);
}
/**
* Photo is successfully retrieved.
* @callback getPicture~onPhotoDataSuccess
* @param {string} imageData - A base64-encoded image.
*/
function onPhotoDataSuccess(imageData) {
//Edit photo
}
我确保质量很低,但速度会变慢,直到崩溃。
谢谢你的帮助!
最后,我通过修复照片大小来解决问题:
/**
* Take a picture and get the image as base64-encoded string.
*/
function capturePhoto() {
setConfig({ quality: 20, targetWidth: 600, targetHeight: 600, correctOrientation: true, destinationType: destinationType.DATA_URL, encodingType: 0});
navigator.camera.getPicture(onPhotoDataSuccess, onFail, _config);
}
"使用Camera.DestinationType.DATA_URL时存在问题。相机图像包含大量数据,将图像转换为字符串并将其传递给Cordova应用程序使用超出了大多数设备的JavaScript引擎的限制。除非你降低图像的质量或大小,否则当你使用此功能时,你很可能会发现应用程序非常慢或崩溃选项"
建议使用FILE_URI,除非你拍的照片很小,否则你永远无法使用DATA_URL。