Question

I've created a simple interaction in Titanium where the user opens an Option Dialog and then selects "Take photo", which opens up the camera.

The problem here is that the app crashes if you do the following:

  1. Open Option Dialog and select "Take photo" which opens up the camera
  2. Hit Cancel
  3. Open the Option Dialog again and select "Take photo" again
  4. Hit Cancel again - the app crashes.

I get the following messages in the console:

Dismissing a view controller when it is not the top presented view controller. Will probably crash now.

attempt to dismiss modal view controller whose view does not currently appear. self = modalViewController =

CoreAnimation: warning, deleted thread with uncommitted CATransaction; set CA_DEBUG_TRANSACTIONS=1 in environment to log backtraces.

MPUSystemMediaControls] Updating supported commands for now playing application.

It looks like this issue has been fixed in native iOS development in Objective C in this Stackoverflow Question, but how can I fixed this with Titanium?

Here is my code

$.imageView.addEventListener("click", function (e) {

    $.imageDialog.cancel = 1;
    $.imageDialog.show();

    $.imageDialog.addEventListener("click", function (event) {
        if (event.index === 0) {
            $.imageDialog.hide();
            Titanium.Media.showCamera({
                success: function (e) {
                    if (e.mediaType === Ti.Media.MEDIA_TYPE_PHOTO) {
                        $.beerImage.image = e.media;  
                        theImage = e.media;  
                        $.cameraImage.opacity = 0.5;
                    }
                },
                cancel: function (e) { console.log('Action was cancelled'); },
                error: function (e)  { console.log('An error happened'); },
                allowEditing: true,
                mediaTypes: [Titanium.Media.MEDIA_TYPE_PHOTO],
                videoQuality: Titanium.Media.QUALITY_HIGH
            });
        }             
    });
});

I have no problems at all if I do not use the Option Dialog and go straight to the Camera. Is is not possible to use both the Option Dialog and the Camera without having this issue? Or have a made a mistake in my code?

Was it helpful?

Solution

I found a work around for this.

The trick is to create a new OptionDialog each time the Camera needs to be opened. In the code below, the Option Dialog gets created when the button is clicked to open the camera. Therefore a new OptionDialog is created, the event listers are added to it and then it is opened.

$.imageView.addEventListener("click", function (e) { 

    var opts = {
      cancel: 2,
      options: ['Take Photo', 'Choose from gallery', 'Cancel'],
      destructive: 0,
      title: 'Choose'
    };

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

    dialog.addEventListener("click", function (event) {
        if (event.index === 0) {
            Titanium.Media.showCamera({
                success: function () { ... },
                cancel: function () { ... },
                error: function () { ... },
                allowEditing: true,
                mediaTypes: [Titanium.Media.MEDIA_TYPE_PHOTO],
                videoQuality: Titanium.Media.QUALITY_HIGH
            });  
        }  
        $.addBeerWin.remove(dialog);          
    });

    dialog.show();
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top