0

Environment: iOS 7.0.3 & 6.1.3, tested on iPhone devices (several separate devices). Ti v 3.1.3 with Alloy 1.2.2.

I'm having trouble with my app freezing when taking a photo on the device. Selecting a photo from the gallery works without a problem, but when taking a photo, the app freezes on the move and scale screen - seemingly, after tapping the "Use" button. My code is as follows:

$.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);
                    }
                });

            }
        });
});

The desired behavior here is that the user taps on their avatar, which opens the option menu. They then tap "Take a photo", which opens the camera allowing them to take the photo and crop it. After tapping "Use", that photo is then displayed in the $.avatar ImageView.

I've been able to reproduce this on every device I've tested on, across multiple iOS versions. Is this a bug in Alloy or TI, or am I doing something overtly wrong here? Thanks!

Justin Davis
  • 305
  • 1
  • 3
  • 16

2 Answers2

0

I am doing the same thing, i have code snippet here hope it can help you:

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);
Mario Galván
  • 3,964
  • 6
  • 29
  • 39
0

The answer to this was incredibly simple, and embarrassingly basic. I'd forgotten to include the break; at the end of each case, causing the switch statement to run through both cases when taking a photo. Adding that in fixed the problem.

-1 for a n00b-level mistake.

Justin Davis
  • 305
  • 1
  • 3
  • 16