Cordova截图不及时工作$ IonicView.Enter

时间:2016-05-03 15:12:23

标签: angularjs cordova ionic-framework cordova-plugins

我们正在开发Ionic的iOS项目。我们希望在离子视图输入(首次进入应用程序时)启动cordova屏幕截图插件,然后使用cordova文件传输发送屏幕截图。

首次进入视图时,以下代码无效。当我们离开视图并返回时,它会发送屏幕截图。

但是,第一次logAction会在第一次进入此视图时触发,而第二次logAction则不会。第二次进入此视图时,将触发两个logActions。

这是我所指代码的一部分:

$scope.$on('$ionicView.enter', function () {
    $scope.logAction({
        "Message": "Login screen succesfully entered",
        "Succeeded": true,
        "transaction": 1,
        "Category": "Info",
        "Device": 0,
    })
  $cordovaScreenshot.capture().then(function(filepath){
    $scope.logAction({
      "Message": filepath,
      "Succeeded": true,
      "transaction": 1,
      "Category": "Info",
      "Device": 0,
    })
    $cordovaScreenshot.send(filepath);
  });
});

这是cordovaScreenshot文件

angular.module('testScreenshots.services', [])
.service('$cordovaScreenshot', ['$q',function ($q) {
    return {
        fileURL: "",
        capture: function (filename, extension, quality) {
          var randomNumber = Math.random();
          console.log("" + randomNumber);
          filename = "testPicture" + randomNumber.toString();
            extension = extension || 'jpg';
            quality = quality || '100';

            var defer = $q.defer();


            navigator.screenshot.save(function (error, res){
                if (error) {
                    console.error(error);
                    defer.reject(error);
                } else {
                    console.log('screenshot saved in: ', res.filePath);
                    this.fileURL = "file://" + res.filePath;
                    defer.resolve(res.filePath);
                    console.log("inside the save function: "+this.fileURL);
                }
            }, extension, quality, filename);

            return defer.promise;
        },


        send: function(filepath){

          var win = function (r) {
            console.log("Code = " + r.responseCode);
            console.log("Response = " + r.response);
            console.log("Sent = " + r.bytesSent);
          }

          var fail = function (error) {
            alert("An error has occurred: Code = " + error.code);
            console.log("upload error source " + error.source);
            console.log("upload error target " + error.target);
          }

          var options = new FileUploadOptions();
          options.fileKey = "file";
          options.fileName = filepath.substr(filepath.lastIndexOf('/') + 1);
          options.mimeType = "multipart/form-data";
          options.chunkedMode = false;
          options.headers = {
            Connection: "close"
          };

          var ft = new FileTransfer();
          ft.upload(filepath, encodeURI("http://192.168.6.165:8080//api/uploadpicture"), win, fail, options);
        }
    };
}])

1 个答案:

答案 0 :(得分:0)

我们在Mac上使用的iOS模拟器存在此问题。尝试在设备上运行后,问题不再影响我们。

因此,这个问题似乎与使用iOS模拟器有关。