1

我向用户展示了一个视图控制器,其中的视图显示了一个 UIButton 来录制视频。当用户按下按钮时,我的应用程序崩溃并出现以下错误:

由于未捕获的异常“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
4

2 回答 2

1

好的,答案终于来了。
https://stackoverflow.com/a/12570501/2133494 基本上我需要向我的 UIImagePickerController 添加一个类别。我尝试了很多其他修复,但这有效。

于 2013-03-10T17:39:04.207 回答
0

您已经实现了 bool 进行自动旋转,但没有指定它是否不自动旋转它还应该做什么。在自动旋转方法之后尝试以下操作。

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

请从上述方法中删除您不需要的任何面具,看看这是否适合您。

于 2013-03-05T22:55:44.397 回答