2

我正在用 ionic 构建一个混合应用程序,它在科尔多瓦上运行。我使用$cordovaCamera 插件从手机中捕获图像,方法是从手机的图库中选择或使用相机拍照。

然后我使用Restangular将该图像发送到我的服务器,当该操作完成时,我想在屏幕上显示一条状态消息。

我的问题:以上所有内容都可以在 Android 上完美运行。在 iOS 上,它在从图库中选择图像时起作用,而不是在直接从手机中捕获图像时起作用。在这种情况下,图像被正确传输到服务器,请求返回 201 Created 就像它应该的那样 - 但then()从未输入回调函数。

如果有人能解释这种行为,那就太棒了……我的第二个最好方法是在 iPhone 上捕获图像,保存到图库,然后尝试检索最后保存的图像,但我无法弄清楚知道如何做,我宁愿让这个工作。

更新:我已将其范围缩小到 Restangular 部分 - 如果我使用 $http 而不是调用 Restangular 上传函数,回调会按预期触发,一切都很好......所以这就是我要做的,但如果有人能告诉我问题出在哪里,我将不胜感激。

相关代码:

/** cameraService functions **/

//when the user chooses to snap a picture from the camera 
takePicture: function(){
            var options = {
                quality: 50,
                destinationType: Camera.DestinationType.DATA_URL,
                sourceType: Camera.PictureSourceType.CAMERA,
                encodingType: Camera.EncodingType.JPEG,
                popoverOptions: CameraPopoverOptions
            };

            return $cordovaCamera.getPicture(options).then(
                function(imageData) {
                    return imageData;
            },
                function(err) {
                    console.log("error", err);
            });
        },

        //when the user chooses to select image from the gallery
        choosePicture: function(){
            var options = {
                destinationType: Camera.DestinationType.DATA_URL,
                sourceType: Camera.PictureSourceType.PHOTOLIBRARY
            };

            return $cordovaCamera.getPicture(options).then(
                function(imageData) {
                    return imageData;
                },
                function(err) {
                    console.log("error", err);
                });
        },
        uploadPicture: function(imageSource, caption, is_logo){
            if (typeof is_logo == 'undefined') is_logo = false;
            var upload_object = {
                caption: caption,
                source: imageSource,
                is_logo: is_logo
            };
//apiService is my wrapper for Restangular
            return apiService.uploadFile(loadingService.getClientUrl('images'), upload_object);

        },


/**apiService uploadFile - apparently the problem is here ***/
uploadFile: function(baseElem, object, route, path){
    var headers = apiFunctions.setHeaders({'Content-Type': undefined});

//this DOES NOT WORK (on iPhone with image from camera) - request completes but callback not triggered 
    return Restangular.all(baseElem).customPOST(object, path, route, headers);
//this works fine:
return $http.post('https://api.mysite.dev/v1/clients/'+localStorageService.getValue('client_id')+'/images', JSON.stringify(object), {headers:headers}
                );

            },


/** controller functions **/

        $scope.takePicture = function () {
            cameraService.takePicture().then(function (imageData) {
                $scope.data.imageSource = imageData;
            });
        };
        $scope.choosePicture = function () {
            cameraService.choosePicture().then(function (imageData) {
                $scope.data.imageSource = imageData;
            });
        };
        $scope.uploadPicture = function () {
            cameraService.uploadPicture($scope.data.imageSource, $scope.data.caption)
                .then(function (response) { //this is never entered if image is captured from camera on iPhone 
                    $ionicScrollDelegate.scrollTop();
                    $scope.data.caption = '';
                    $scope.data.imageSource = '';
                    if (response.data.response.is_success.data_value == true) {
                        $scope.messages.success.push("Photo uploaded successfully");
                    } else {
                        $scope.messages.failure.push("Error uploading photo.");
                    }
                });
        }
4

0 回答 0