2012-01-26 15 views
17

Witam pracuję nad aplikacją uniwersalną (iPhone/iPad). jedną z funkcji jest to, że muszę wybrać zdjęcie z albumu i pokazać je na UIImageView.Jak korzystać z UIImagePickerController na iPadzie?

Problem polega na tym, że działa dobrze na iPhonie, ale kiedy próbuję otworzyć album fotograficzny, ulega awarii. mój kod w arkuszu czynnościowym jest następujący:

- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){ 
     if (([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])) 

     { 
      if (buttonIndex == 0) 
      { 
       [self lockAllImagesOnTheScreen]; 
       imagePicker.sourceType=UIImagePickerControllerSourceTypeCamera; 
       [self presentModalViewController:imagePicker animated:YES]; 
      } 
      if (buttonIndex == 1) 
      { 
       [self lockAllImagesOnTheScreen]; 

       imagePicker.sourceType= UIImagePickerControllerSourceTypePhotoLibrary; 
       [self presentModalViewController:imagePicker animated:YES]; 
      } 

     } 
     else { 

      if (buttonIndex == 0) 
      { 
       [self lockAllImagesOnTheScreen]; 
       imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; 
       imagePicker.sourceType= UIImagePickerControllerSourceTypePhotoLibrary; 
       [self presentModalViewController:imagePicker animated:YES]; 
      } 
     } 



    } 

    else{ 
     if (([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])) 

     { 
      if (buttonIndex == 0) 
      { 
       [self lockAllImagesOnTheScreen]; 
       imagePicker.sourceType=UIImagePickerControllerSourceTypeCamera; 
       [self presentModalViewController:imagePicker animated:YES]; 
      } 
      if (buttonIndex == 1) 
      { 
       [self lockAllImagesOnTheScreen]; 

       imagePicker.sourceType= UIImagePickerControllerSourceTypePhotoLibrary; 
       [self presentModalViewController:imagePicker animated:YES]; 
      } 

     } 
     else { 

      if (buttonIndex == 0) 
      { 
       [self lockAllImagesOnTheScreen]; 
       imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; 
       imagePicker.sourceType= UIImagePickerControllerSourceTypePhotoLibrary; 
       [self presentModalViewController:imagePicker animated:YES]; 
      } 
     } 


    } 


} 

Czy jakiekolwiek ciało może mi pomóc? Sprawdziłem na StackOverflow, a także googled, ale na próżno.

+0

Jeśli ktokolwiek poświęci, to plz również napiszę powód, cz próbowałem 1, aby to rozgryźć, ale nic mi nie pomogło, to dlaczego zadałem to pytanie tutaj – Mashhadi

+0

Co to jest wyjątek? Jeśli to pamięć, czy wypróbowałeś NSZombiesEnabled? – gerry3

+0

"Program otrzymał sygnał SIGABRT" jest to wyjątek – Mashhadi

Odpowiedz

56

UIImagePickerControllermusi być prezentowane z UIPopoverController na iPadzie.

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) { 
    UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:picker]; 
    [popover presentPopoverFromRect:self.selectedImageView.bounds inView:self.selectedImageView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; 
    self.popOver = popover; 
} else { 
    [self presentModalViewController:picker animated:YES]; 
} 

EDIT: Dodaj silną właściwość dla UIPopoverController:

@property (nonatomic, strong) UIPopoverController *popOver; 

popover należy oddalić w metodach Delegat:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker 
+0

ta odpowiedź jest niekompletna. czym jest self.popOver? nie ma jej definicji w przedstawionym kodzie –

+7

To nie jest tak niekompletne, wystarczy przeczytać między wierszami ... Oczywiście self.popOver jest własnością typu 'UIPopoverController', który prawdopodobnie jest używany do obsługi zamykania popupu później . Po prostu mówię " – mprivat

+3

Nie, jest niekompletne. Jeśli mamy "czytać między wierszami", to potrzebujemy przynajmniej kierunku "czytania". To jest forum do nauki. Gratulacje za doświadczenie iOs, ale nie musisz być niegrzeczny. – edthethird

5

Docs Jabłko powiedzieć

"Present the user interface by calling the presentViewController:animated:completion: method of the currently active view controller, passing your configured image picker controller as the new view controller. On iPad, present the user interface using a popover. Doing so is valid only if the sourceType property of the image picker controller is set to UIImagePickerControllerSourceTypeCamera."

To znaczy dokładnie odwrotnie, jak się zachowuje?!? Ty CANT prezentujesz UIImagePickerControllerSourceTypeCamera z popover, a Ty CANT prezentujesz UIImagePickerControllerSourceTypePhotoLibrary i UIImagePickerControllerSourceTypeSavedPhotosAlbum modalnie.

Dziwne ...

6

Tutaj pokażę ci drogę SWIFT:

import UIKit 
class StoreItemViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate 
{ 
    @IBOutlet weak var button: UIButton! 
    @IBOutlet weak var productImage: UIImageView! 
    var popOver:UIPopoverController? 

    @IBAction func buttonSelected(sender:UIButton) 
    { 
     if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum) 
     { 
      var imagePickerController = UIImagePickerController() 
      imagePickerController.delegate = self 
      imagePickerController.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum 
      imagePickerController.allowsEditing = false 

      if UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Pad 
      { 
       self.popOver = UIPopoverController(contentViewController: imagePickerController) 
       self.popOver?.presentPopoverFromRect(self.productImage.bounds, inView: self.productImage, permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true)  
      } 
      else 
      { 
       self.presentViewController(imagePickerController, animated: true, completion: { imageP in 

       }) 
      } 
     } 
    } 

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) { 
     //do anything with the image 
     let selectedImage = info[UIImagePickerControllerOriginalImage] as UIImage 

     //closing the popup 
     popOver?.dismissPopoverAnimated(true) 

    } 

    func imagePickerControllerDidCancel(picker: UIImagePickerController) 
    { 
     println("cancel") 

     //closing the popup 
     popOver?.dismissPopoverAnimated(true) 
    } 
} 
2

POST iOS 8: spróbuj dodać kontroler popover w

[[NSOperationQueue mainQueue] addOperationWithBlock:^{ }];

Powód: Jest ponieważ w systemie iOS 8 widoki alertów i arkusze czynności są faktycznie przedstawionymi kontrolerami widoku (UIAlertController). Tak więc, jeśli prezentujesz nowy kontroler widoku w odpowiedzi na akcję z UIAlertView, jest prezentowany, gdy UIAlertController jest odrzucany. Musisz to zrobić w głównej kolejce bez zakłócania nawigacji.

0

Jeśli urządzeniem jest iPad, a typ źródła jest określony jako "photoLibrary" lub "savedPhotosAlbum", UIImagePickerController musi być przedstawiony jako popover zgodnie z dokumentami Apple. W moim przypadku osiągam taki cel;

func choosePhotoFromLibrary() { 
    let imagePicker = UIImagePickerController() 
    imagePicker.sourceType = .photoLibrary 
    imagePicker.delegate = self 
    imagePicker.allowsEditing = true 

    if UIDevice.current.userInterfaceIdiom == .pad { 
     imagePicker.modalPresentationStyle = .popover 
     present(imagePicker, animated: true, completion: nil) 

     let imagePickerPopOverPresentationController = imagePicker.popoverPresentationController 
     imagePickerPopOverPresentationController?.permittedArrowDirections = .up 

     let photoPickingTableCell = tableView.cellForRow(at: IndexPath(row: 2, section: 0)) 

     imagePickerPopOverPresentationController?.sourceView = photoPickingTableCell 
     imagePickerPopOverPresentationController?.sourceRect = profilePhotoImageView.frame 
    } 
    else { 
     present(imagePicker, animated: true, completion: nil) 
    } 
} 

Mam komórkę widoku tabeli, która zawiera widok obrazu. Wybór tej komórki wymaga otwarcia kontrolera selektora obrazu, dlatego wywołuje metodę choosePhotoFromLibrary. W tej metodzie, jeśli urządzeniem jest podkładka, należy przypisać popover stylu prezentacji ikony i zaprezentować ją. Następnie skonfiguruj zachowanie popover. W moim przypadku, mój sourceView jest komórką tabeli, która zawiera widok obrazu, a mój sourceRect jest ramką widoku obrazu.

Możesz także skorzystać z metod UIPopoverPresentationControllerDelegate, jeśli przypiszesz delegata.

Powiązane problemy