iPad與iPhone調(diào)用UIImagePickerViewController方法略有不同是本文要介紹的內(nèi)容,文中很詳細(xì)的講述了iPad與iphone各自的調(diào)用方法,來(lái)看詳細(xì)內(nèi)容。
我們知道,在iPhone中獲取照片庫(kù)常用的方法如下:
- UIImagePickerController *m_imagePicker = [[UIImagePickerController alloc] init];
- if ([UIImagePickerController isSourceTypeAvailable:
- UIImagePickerControllerSourceTypePhotoLibrary]) {
- m_imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
- m_imagePicker.delegate = self;
- // [m_imagePicker.navigationBar.subviews];
- [m_imagePicker setAllowsEditing:NO];
- //m_imagePicker.allowsImageEditing = NO;
- [self presentModalViewController:m_imagePicker animated:YES];
- [m_imagePicker release];
- }else {
- UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:
- @"Error accessing photo library!" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil];
- [alert show];
- [alert release];
- }
這對(duì)iPhone的操作是沒(méi)有問(wèn)題的。但是當(dāng)我們?cè)趇Pad環(huán)境中卻有問(wèn)題了,當(dāng)我們運(yùn)行時(shí)會(huì)報(bào)如下錯(cuò)誤:
- Terminating app due to uncaught exception 'NSInvalidArgumentException',
- reason: 'On iPad, UIImagePickerController must be presented via UIPopoverController'
所以我們必須通過(guò)UIPopoverController來(lái)實(shí)現(xiàn)才行。具體實(shí)現(xiàn)如下:
- UIImagePickerController *m_imagePicker = [[UIImagePickerController alloc] init];
- if ([UIImagePickerController isSourceTypeAvailable:
- UIImagePickerControllerSourceTypePhotoLibrary]) {
- m_imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
- m_imagePicker.delegate = self;
- [m_imagePicker setAllowsEditing:NO];
- UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:m_imagePicker];
- self.popoverController = popover;
- //popoverController.delegate = self;
-
- [popoverController presentPopoverFromRect:CGRectMake(0, 0, 300, 300) inView:self.
- view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
-
- //[self presentModalViewController:m_imagePicker animated:YES];
- [popover release];
- [m_imagePicker release];
- }else {
- UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"Error accessing photo library!"
- delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil];
- [alert show];
- [alert release];
- }
這里需要注意,對(duì)局部UIPopoverController對(duì)象popover我們賦給了一個(gè)全局的UIPopoverController對(duì)象popoverController。而不能直接調(diào)用popover。因?yàn)樵趐opover對(duì)象還可見(jiàn)時(shí),是不能夠被釋放的。
小結(jié):iPad與iphone調(diào)用UIImagePickerViewController方法略有不同的內(nèi)容介紹完了,希望本文對(duì)你有所幫助!
|