Pergunta

I use UIImagePickerController for using Camera and Camera Roll in my project.

This is my example code

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

[self presentViewController:picker animated:YES completion:NULL];

It worked but if i want to dismiss it when i enter to background mode. how to fix it.

Thank you and sorry for my english.

Foi útil?

Solução

You can listen to the UIApplicationDidEnterBackgroundNotification notification using the NSNotificationCenter.

With that, you can setup a method to be called whenever the app goes into the background where you can check if the picker is being displayed (perhaps store a reference to it whenever it is being displayed):

- (void)presentImagePicker
{
    if (self.displayedPicker) {
        return;
    }

    self.displayedPicker = [[UIImagePickerController alloc] init];
    self.displayedPicker.delegate = self;
    self.displayedPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    [self presentViewController:self.displayedPicker animated:YES completion:nil];

    // Register for the method to be called every time the app enters the background
    [[NSNotificationCenter defaultCenter]
        addObserver:self
        selector:@selector(didEnterBackgroundNotification:)
        name:UIApplicationDidEnterBackgroundNotification
        object:nil
    ];
}

- (void)dismissImagePickerAnimated:(BOOL)animated
{
    if (self.displayedPicker) {
        [self.displayedPicker dismissViewControllerAnimated:animated];
        self.displayedPicker = nil;

        // Unregister from notification
        [[NSNotificationCenter defaultCenter]
            removeObserver:self
            name:UIApplicationDidEnterBackgroundNotification
            object:nil
        ];
    }
}

- (void)didEnterBackgroundNotification:(NSNotification *)notification
{
    [self dismissImagePickerAnimated:NO];
}

Outras dicas

You have to make the UIImagePickerController a property in your class. In your AppDelegate Class you can implement the applicationDidEnterBackground: Method in wich you can put something like this:

[yourViewController.picker dismissModalViewControllerAnimated:YES];

This should dismiss the ImagePicker if your app enters the background. Hope this helps.. :)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top