2013-03-05 199 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