iPad與iPhone調用UIImagePickerViewController方法

來源:互聯網
上載者:User

iPadiPhone調用UIImagePickerViewController方法略有不同是本文要介紹的內容,文中很詳細的講述了iPadiphone各自的調用方法,來看詳細內容。

我們知道,在iPhone中擷取照片庫常用的方法如下:

 
  1. UIImagePickerController *m_imagePicker = [[UIImagePickerController alloc] init];  
  2.     if ([UIImagePickerController isSourceTypeAvailable:  
  3.          UIImagePickerControllerSourceTypePhotoLibrary]) {  
  4.         m_imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;  
  5.         m_imagePicker.delegate = self;  
  6.         //        [m_imagePicker.navigationBar.subviews];  
  7.         [m_imagePicker setAllowsEditing:NO];  
  8.         //m_imagePicker.allowsImageEditing = NO;  
  9.         [self presentModalViewController:m_imagePicker animated:YES];  
  10.         [m_imagePicker release];  
  11.     }else {  
  12.         UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:  
  13. @"Error accessing photo library!" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil];  
  14.         [alert show];  
  15.         [alert release];  
  16.     } 

這對iPhone的操作是沒有問題的。但是當我們在iPad環境中卻有問題了,當我們運行時會報如下錯誤:

 
  1. Terminating app due to uncaught exception 'NSInvalidArgumentException',   
  2. reason: 'On iPad, UIImagePickerController must be presented via UIPopoverController' 

所以我們必須通過UIPopoverController來實現才行。具體實現如下:

 
  1. UIImagePickerController *m_imagePicker = [[UIImagePickerController alloc] init];  
  2.     if ([UIImagePickerController isSourceTypeAvailable:  
  3.          UIImagePickerControllerSourceTypePhotoLibrary]) {  
  4.         m_imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;  
  5.         m_imagePicker.delegate = self;  
  6.         [m_imagePicker setAllowsEditing:NO];  
  7.         UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:m_imagePicker];  
  8.         self.popoverController = popover;  
  9.         //popoverController.delegate = self;  
  10.            
  11.         [popoverController presentPopoverFromRect:CGRectMake(0, 0, 300, 300) inView:self.
  12. view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];  
  13.            
  14.         //[self presentModalViewController:m_imagePicker animated:YES];  
  15.         [popover release];  
  16.         [m_imagePicker release];  
  17.     }else {  
  18.         UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"Error accessing photo library!" 
  19. delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil];  
  20.         [alert show];  
  21.         [alert release];  
  22.     } 

這裡需要注意,對局部UIPopoverController對象popover我們賦給了一個全域的UIPopoverController對象popoverController。而不能直接調用popover。因為在popover對象還可見時,是不能夠被釋放的。

小結:iPadiphone調用UIImagePickerViewController方法略有不同的內容介紹完了,希望本文對你有所協助!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.