【问题标题】:Titanium App Freezes When Taking Photo on iOS在 iOS 上拍照时 Titanium 应用程序冻结
【发布时间】:2013-11-17 14:53:22
【问题描述】:

环境:iOS 7.0.3 和 6.1.3,在 iPhone 设备(几个单独的设备)上测试。 Ti v 3.1.3 与合金 1.2.2。

在设备上拍照时,我的应用卡住了。从图库中选择照片没有问题,但是在拍照时,应用程序会在移动和缩放屏幕上冻结 - 似乎是在点击“使用”按钮后。我的代码如下:

$.avatar.addEventListener("click", function() {

    var opts = {
        cancel : 2,
        options : ['Take a Photo', 'Select from Gallery', 'Cancel'],
        selectedIndex : 2,
        destructive : 0,
        title : 'Set a Profile Photo:'
    };

    var dialog = Ti.UI.createOptionDialog(opts);
    dialog.show();

    dialog.addEventListener("click", function(e) {
        switch(e.index) {

            case(0):
                Titanium.Media.showCamera({
                    allowEditing : true,
                    success : function(event) {
                        $.avatar.status = "new";
                        $.avatar.image = event.media;
                    },

                    error : function(event) {
                        console.log(event);
                    }
                });

            case(1):
                Titanium.Media.openPhotoGallery({
                    allowEditing : true,
                    success : function(event) {
                        $.avatar.status = "new";
                        $.avatar.image = event.media;
                    },

                    error : function(event) {
                        console.log(event);
                    }
                });

            }
        });
});

此处所需的行为是用户点击他们的头像,这会打开选项菜单。然后他们点击“拍照”,这会打开相机,让他们拍照并裁剪。点击“使用”后,该照片会显示在 $.avatar ImageView 中。

我已经能够在我测试过的每台设备上跨多个 iOS 版本重现这一点。这是 Alloy 或 TI 中的错误,还是我在这里做错了什么? 谢谢!

【问题讨论】:

    标签: titanium titanium-alloy


    【解决方案1】:

    我也在做同样的事情,我这里有代码sn-p希望它可以帮助你:

    var options = {
    
                            success: function(e) { 
                                // var tempDate = new Date().getTime();
                                var completeSubfix = rcvImage.subfix + APP.uniqueIdCounter(sectionId);
                                var noDashPlatformId = Titanium.Platform.id.replace(/-/g,"");
                                var imageName = result[0].parentAccount + "-" +result[0].id_user + "-" + noDashPlatformId + "-" + completeSubfix;
                                urls[rcvImage.id] = Ti.Filesystem.applicationDataDirectory+ imageName+ '.png';
    
                                var tmpPhoto = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, imageName + '.png');
                                tmpPhoto.write(e.media);
    
                                var useHeight = true;
    
                                if(OS_ANDROID){
                                    //If in portrait, we use this variable to invert the cropping.
                                    useHeight = e.cropRect.height <  e.cropRect.width ? true : false; 
                                }
    
                                //Creating thumbnail for both OS
                                var thumbnailtmp = e.media.imageAsThumbnail(180, 1,2);
    
    
                                if(OS_ANDROID){
                                    //Making it a rectangle again so it wont get streched on the image view
                                    thumbnailtmp =  thumbnailtmp.imageAsCropped({
                                        width:  useHeight ?  180 : 122, 
                                        height: useHeight ?  122 : 180,
                                        x:0,
                                        y:0
                                    }); 
                                }
    
    
    //                                      
                                var thumbnail = Ti.UI.createImageView({
                                    image: thumbnailtmp
                                });
    
                                var tmpPhoto = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, imageName + '_thum.png');
                                tmpPhoto.write(thumbnail.image);
    
                                rcvImage.image = tmpPhoto.nativePath;
                                //rcvImage.image = APP.getThumbnailMedium(urls[rcvImage.id] ,"medium");
                                if(OS_IOS){
                                    rcvImage.children[0].opacity = 0.6;
                                    rcvImage.children[1].text = completeSubfix;
                                }
                                else{
                                    rcvImage.parent.children[1].opacity = 0.6;  
                                    rcvImage.parent.children[2].text = completeSubfix;                          
                                }
                                _callback(rcvImage);
    
                            },
    
                            cancel:function() {
                            // called when user cancels taking a picture
                            },
                            error:function(error) {
                                // called when there's an error
                                var a = Titanium.UI.createAlertDialog({title:'Camera'});
                                if (error.code == Titanium.Media.NO_CAMERA) {
                                    a.setMessage('Please run this test on device');
                                } else {
                                    a.setMessage('Unexpected error: ' + error.code);
                                }
                                a.show();
                            },
    
                };
    
    
                    Ti.Media.showCamera(options);
    

    【讨论】:

      【解决方案2】:

      这个问题的答案非常简单,而且非常基本。我忘记在每个案例的末尾包含break;,导致在拍照时 switch 语句在这两个案例中都运行。添加它解决了这个问题。

      -1 表示 n00b 级错误。

      【讨论】:

        猜你喜欢
        • 2023-04-04
        • 2016-07-29
        • 1970-01-01
        • 2012-05-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-17
        相关资源
        最近更新 更多