2013-03-05 120 views
1

我向用户展示了一个视图控制器,该视图显示了一个用于记录视频的UIButton。当用户按下按钮时,我的应用程序崩溃,出现以下错误:UIImagePickerController在启动时崩溃

由于未捕获异常'UIApplicationInvalidInterfaceOrientation',终止应用程序,原因:'preferredInterfaceOrientationForPresentation必须返回受支持的界面方向!

我的应用只支持纵向,info.plist文件正确反映。我在Ray Wenderlich网站的另一个应用中使用了相同的代码,并且效果很好。下面是.h和.m文件的代码。任何帮助,将不胜感激。

.H

#import <MediaPlayer/MediaPlayer.h> 
#import <MobileCoreServices/UTCoreTypes.h> 
#import <AssetsLibrary/AssetsLibrary.h> 

@interface RecordSwingViewController: UIViewController 

-(BOOL)startCameraControllerFromViewController:(UIViewController*)controller 
           usingDelegate:(id)delegate; 
-(void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void*)contextInfo; 



@property (weak, nonatomic) IBOutlet UIButton *record; 
- (IBAction)recordSwing:(id)sender; 

@end 

.M

#import "RecordSwingViewController.h" 

@interface RecordSwingViewController() 

@end 

@implementation RecordSwingViewController 



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (IBAction)recordSwing:(id)sender { 
    [self startCameraControllerFromViewController:self usingDelegate:self]; 

} 
-(BOOL)shouldAutorotate 
{ 
    return NO; 
} 



-(BOOL)startCameraControllerFromViewController:(UIViewController*)controller 
           usingDelegate:(id)delegate { 
    // 1 - Validattions 
    if (([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == NO) 
     || (delegate == nil) 
     || (controller == nil)) { 
     return NO; 
    } 
    // 2 - Get image picker 
    UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init]; 
    cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera; 
    // Displays a control that allows the user to choose movie capture 
    cameraUI.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil]; 
    // Hides the controls for moving & scaling pictures, or for 
    // trimming movies. To instead show the controls, use YES. 
    cameraUI.allowsEditing = NO; 
    cameraUI.delegate = delegate; 
    // 3 - Display image picker 
    [controller presentViewController: cameraUI animated: YES completion:nil]; 
    return YES; 
} 
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 
    NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType]; 
    [self dismissViewControllerAnimated:NO completion:nil]; 
    // Handle a movie capture 
    if (CFStringCompare ((__bridge_retained CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo) { 
     NSString *moviePath = [[info objectForKey:UIImagePickerControllerMediaURL] path]; 
     if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(moviePath)) { 
      UISaveVideoAtPathToSavedPhotosAlbum(moviePath, self, 
               @selector(video:didFinishSavingWithError:contextInfo:), nil); 
     } 
    } 
} 

-(void)video:(NSString*)videoPath didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo { 
    if (error) { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Video Saving Failed" 
                 delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [alert show]; 
    } else { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Video Saved" message:@"Saved To Photo Album" 
                 delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [alert show]; 
    } 
} 
@end 

回答

0

您已经实现了自动旋转的布尔,但如果它不自动没有指定旋转它应该做些什么。 autorotate方法后尝试以下操作。

-(NSUInteger)supportedInterfaceOrientations 
{ 
return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown; 
} 

请删除上述方法中不需要的任何掩码,看看它是否适用于您。

+0

感谢您的回答,不幸的是,我仍然收到错误,我尝试添加代码,并将其全部注释掉,并且没有任何更改。 - (BOOL)shouldAutorotate { return NO; } - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; } – 2013-03-06 16:17:38