2013-07-16 79 views
0

我为我试图打开图像拾取一个巨大的问题,但不知道为什么,在这条线我的应用程序崩溃如何从手机图库中获取照片?

[self presentViewController:(UIViewController *)imgpkrController animated:YES completion:nil]; 

我已经使用这个代码,这在许多应用程序和代码工作正常,但在我目前的应用程序此代码给出了首选接口方向的例外

使用该代码:

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ 
if (cameraAvailable == TRUE) { 
    if (buttonIndex == 0) { 
     imgpkrController.delegate = self; 
     imgpkrController.allowsEditing = YES; 
     imgpkrController.sourceType = UIImagePickerControllerSourceTypeCamera; 
     //[self presentModalViewController:(UIViewController *)imgpkrController animated:YES]; 
     [self presentViewController:(UIViewController *)imgpkrController animated:YES completion:nil]; 
    } 
    else if(buttonIndex == 1){ 
     imgpkrController.delegate = self; 
     imgpkrController.allowsEditing = YES; 
     imgpkrController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
     //[self presentModalViewController:(UIViewController *)imgpkrController animated:YES]; 
     [self presentViewController:(UIViewController *)imgpkrController animated:YES completion:nil]; 
    } 
} 
if (buttonIndex == 0) { 
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) 
    { 
      imgpkrController.delegate = self; 
      imgpkrController.allowsEditing = YES; 
      imgpkrController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
      //imgpkrController.modalPresentationStyle=UIModalPresentationFullScreen; 
      //[self presentModalViewController:imgpkrController animated:YES]; 
      [self presentViewController:(UIViewController *)imgpkrController animated:YES completion:nil]; 
      //[self presentViewController:imgpkrController animated:YES completion:nil]; 
    } 
    else{ 
     NSLog(@"ipad photo"); 
     imgpkrController.delegate = self; 
     popoverController = [[UIPopoverController alloc] initWithContentViewController:imgpkrController]; 
     [popoverController presentPopoverFromRect:CGRectMake(260.0, 0.0, 400.0, 570.0) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; 
    } 
} 
else{ 
    if (buttonIndex == 0) { 
     if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) 
     { 
      imgpkrController.delegate = self; 
      imgpkrController.allowsEditing = YES; 
      imgpkrController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
      //[self presentModalViewController:(UIViewController *)imgpkrController animated:YES]; 
      [self presentViewController:(UIViewController *)imgpkrController animated:YES completion:nil]; 
     }else{ 
      NSLog(@"ipad photo"); 
      imgpkrController.delegate = self; 
      popoverController = [[UIPopoverController alloc] initWithContentViewController:imgpkrController]; 
      [popoverController presentPopoverFromRect:CGRectMake(260.0, 0.0, 400.0, 570.0) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; 
     } 
    } 
} 

}

在这条线

[self presentViewController:(UIViewController *)imgpkrController animated:YES completion:nil]; 

代码崩溃,出现异常:

2013-07-16 15:11:02.143 HMW[1335:c07] *** Terminating app due to uncaught exception  'UIApplicationInvalidInterfaceOrientation', reason:  'preferredInterfaceOrientationForPresentation must return a supported interface orientation!' 
*** First throw call stack: 
(0x2ec5012 0x2a7ce7e 0x2ec4deb 0x17a54da 0x19a444c 0x17a24f3 0x17a2777 0x2a663 0x1a952c1 0x2a90705 0x16bb920 0x16bb8b8 0x177c671 0x177cbcf 0x177bd38 0x16eb33f 0x16eb552 0x16c93aa 0x16bacf8 0x38a7df9 0x38a7ad0 0x2e3abf5 0x2e3a962 0x2e6bbb6 0x2e6af44 0x2e6ae1b 0x38a67e3 0x38a6668 0x16b865c 0x2802 0x2735) 
libc++abi.dylib: terminate called throwing an exception 
+0

你的'shouldAutorotate'返回什么?你的'preferredInterfaceOrientationForPresentation'返回了什么? –

+0

@JoachimIsaksson我应该autorotate返回“是”,而我没有在我的应用程序中设置任何首选的界面方向。 –

+0

如果您有'shouldAutorotate'返回YES,您需要(至少在某些控件中)设置首选方向。考虑到消息“preferredInterfaceOrientationForPresentation必须返回一个支持的接口方向!”我怀疑你正在击中其中的一个。 –

回答

0

不知道是什么导致你的错误消息为这似乎是与设备方向,但我设法得到这个工作,用下面的代码:

- (IBAction)attachPhotosClicked:(id)sender 
{ 
    if (![UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]) 
    {   
     _imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
     [_popoverController presentPopoverFromRect:self.attachButton.frame inView:self.view 
          permittedArrowDirections:(UIPopoverArrowDirectionLeft) 
              animated:YES]; 
     return; 
    } 
    else 
    {   
     UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Choose a source" 
                   delegate:self 
                 cancelButtonTitle:@"Cancel" 
                destructiveButtonTitle:nil 
                 otherButtonTitles:@"Camera", @"Photo Library", nil]; 
     [actionSheet showFromRect:self.attachButton.frame inView:self.view animated:YES]; 
    } 
} 
0
- (IBAction)selectPhoto:(UIButton *)sender { 

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

[self presentViewController:picker animated:YES completion:NULL]; 
} 
相关问题