Używanie interfejsu UIImagePickerController w orientacji poziomej

Tworzę aplikację, która jest w trybie krajobrazowym i używam UIImagePickerController do robienia zdjęć za pomocą aparatu iPhone w nim i chcę ją również utworzyć w trybie krajobrazowym.

Ale jak sugeruje dokumentacja Apple UIImagePickerController nie obsługuje orientacji poziomej, więc co powinienem zrobić, aby uzyskać pożądaną funkcjonalność?

Author: iain, 2013-10-15

8 answers

Jeśli chcesz używać interfejsu UIImagePickerController w trybie poziomym, użyj odpowiedzi user1673099 , ale zamiast:

- (BOOL)shouldAutorotate
{
    return NO;
}

Użycie:

- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskLandscape;
}

I wtedy selektor otworzy się w trybie poziomym:

Tutaj wpisz opis obrazka

Ale upewnij się, że sprawdzasz portret w Info o wdrożeniu:

Tutaj wpisz opis obrazka

 46
Author: Mc.Lover,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-01 13:10:27

... i chcę stworzyć go również w trybie poziomym.

Jedna linijka kodu może zrobić wielką różnicę! W metodzie lub funkcji, w której ląduje Twoja IBAction:

W Języku Swift,

let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
// .overCurrentContext allows for landscape and portrait mode
imagePickerController.modalPresentationStyle = .overCurrentContext

Objective-C,

UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
[imagePickerController setDelegate:self];
[imagePickerController setModalPresentationStyle: UIModalPresentationOverCurrentContext];

Uwaga: pozwoli to imagePickerController poprawnie przedstawić swój widok, ale Może nie rozwiązywać problemu rotacji podczas jej prezentowania.

 19
Author: David,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-28 18:11:55

Spróbuj tędy....

Zgodnie z dokumentem Apple, Kontroler ImagePicker nigdy nie obraca się w trybie poziomym. Musisz używać tylko w trybie portretowym.

Aby wyłączyć tryb krajobrazowy tylko dla kontrolera ImagePicker, wykonaj poniższy kod:

W Twoim Viewcontrolle.m:

Tworzy podklasę (NonRotatingUIImagePickerController) kontrolera pickera obrazu

@interface NonRotatingUIImagePickerController : UIImagePickerController

@end

@implementation NonRotatingUIImagePickerController
// Disable Landscape mode.
- (BOOL)shouldAutorotate
{
    return NO;
}
@end

Użyj zgodnie z instrukcją

UIImagePickerController* picker = [[NonRotatingUIImagePickerController alloc] init];
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        picker.delegate = self; 
  etc.... Just as Default ImagePicker Controller

To działa dla mnie & Daj mi znać, jeśli masz jakieś Problem.

 12
Author: user1673099,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-10-21 10:47:59

Poprawnym sposobem użycia UIImagePickerController w trybie poziomym bez hacków jest umieszczenie go w UIPopoverController

- (void)showPicker:(id)sender
{
    UIButton *button = (UIButton *)sender;
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    _popover = [[UIPopoverController alloc] initWithContentViewController:picker];
    [_popover presentPopoverFromRect:button.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
 5
Author: iain,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-07-28 11:48:08

Oto wersja, która obsługuje rotację we wszystkich orientacjach interfejsu:

/// Not fully supported by Apple, but works as of iOS 11.
class RotatableUIImagePickerController: UIImagePickerController {

  override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .all
  }
}

W ten sposób, jeśli użytkownik obróci swoje urządzenie, zaktualizuje kontroler picker, aby obsługiwał bieżącą orientację. Wystarczy utworzyć instancję tak, jak normalnie byłoby UIImagePickerController.

Jeśli chcesz obsługiwać tylko podzbiór orientacji, możesz zwrócić inną wartość.

 5
Author: Senseful,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-10-26 20:09:05

Jest to bardzo proste i łatwe w obsłudze.

import UIKit

extension UIImagePickerController {
    override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .all
    }
}

Po prostu upuść rozszerzenie gdzieś w swoim projekcie, nie musisz niczego podklasować, aby działało.

Jeśli musisz określić typy urządzeń, możesz dodać czek w następujący sposób:

import UIKit

extension UIImagePickerController {
    override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return UIDevice.current.userInterfaceIdiom == .phone ? .portrait : .all
    }
}

Pozwoli to na swobodne obracanie iPada, ale wymusi tryb portretowy na telefonie. Po prostu upewnij się, że aplikacja jest skonfigurowana do obsługi tych informacji.plist, w przeciwnym razie możesz napotkać awarie po uruchomieniu picker.

 5
Author: CodeBender,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-10-26 22:41:34

Modify above code method

- (NSUInteger)supportedInterfaceOrientations
 {
     UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
     if(orientation == UIDeviceOrientationLandscapeRight || orientation == UIDeviceOrientationLandscapeLeft)
         return UIInterfaceOrientationMaskLandscape;
     else
         return UIInterfaceOrientationMaskPortrait;
 }
 3
Author: Abhishek Thapliyal,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-24 12:45:15

Zaakceptowana odpowiedź nie działa na mnie. Musiałem również dodać modalPresentationStyle do UIImagePickerController, aby to działało.

UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
pickerController.modalPresentationStyle = UIModalPresentationCurrentContext; //this will allow the picker to be presented in landscape
pickerController.delegate = self;
pickerController.allowsEditing = YES;
pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:pickerController animated:YES completion:nil];

I oczywiście pamiętaj, aby umieścić to w kontrolerze, który prezentuje picker:

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape; //this will force landscape
}

Ale zgodnie z dokumentacją Apple nie jest to obsługiwane, aby zaprezentować ten selektor w trybie poziomym, więc uważaj na to.

 2
Author: Michal Cichon,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-07-14 10:08:03