1

我有一个基本上是视图的应用程序,当用户单击按钮时,相机可视化开始。

我想在未显示相机时允许所有方向,但是当显示相机时,我需要强制应用程序进入纵向模式,因为如果它不是纵向模式,则视频会旋转。当相机关闭时,应用程序可能会再次旋转。

你知道我是否可以解决视频方向的问题吗?

或者我怎样才能强制应用程序进入纵向模式?我知道在早期的 ios 版本上,您可以使用[UIDevice setOrientation:],但对于最新的 ios,它已被弃用。

如何为 ios 5 和 ios 6 执行此操作?

我试过:

[self presentViewController:[UIViewController new] animated:NO 
completion:^{ [self dismissViewControllerAnimated:NO completion:nil]; }]; 

而在 shouldAutorotateToInterfaceOrientation 方法中:

if (state == CAMERA) {
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }else{
        return YES;
}

它可以正常工作并强制应用程序进行纵向显示。但是当相机关闭时,它不能正常工作,它不能很好地旋转。

我的意思是,当相机关闭时,会发生以下情况:

  • 该应用程序是纵向的
  • 如果我尝试旋转应用程序,则设备会旋转,但应用程序不会旋转。我可以看到带有时间、电池等信息的 ipad 状态栏已旋转,但应用程序没有。
  • 如果我再次进入纵向,然后旋转设备,它可以正常工作。

你知道可能是什么问题吗?

提前致谢。

4

2 回答 2

3

我想我已经找到了同时改变相机和设备方向的解决方案。

我在初始化相机时调用此代码,并且在我允许所有方向的 shouldAutorotateToInterfaceOrientation 方法中也调用此代码。

 AVCaptureVideoOrientation newOrientation;

    UIInterfaceOrientation deviceOrientation = [UIApplication sharedApplication].statusBarOrientation;

    NSLog(@"deviceOrientation %c",deviceOrientation);

    switch (deviceOrientation)
    {
        case UIInterfaceOrientationPortrait:
            NSLog(@"UIInterfaceOrientationPortrait");
            newOrientation = AVCaptureVideoOrientationPortrait;
            break;
        case UIInterfaceOrientationLandscapeRight:
            NSLog(@"UIInterfaceOrientationLandscapeRight");
            newOrientation = AVCaptureVideoOrientationLandscapeRight;
            break;
        case UIInterfaceOrientationLandscapeLeft:
            NSLog(@"UIInterfaceOrientationLandscapeLeft");
            newOrientation = AVCaptureVideoOrientationLandscapeLeft;
            break;
        default:
            NSLog(@"default");
            newOrientation = AVCaptureVideoOrientationPortrait;
            break;
    }

    if ([self.prevLayer respondsToSelector:@selector(connection)]){
        if ([self.prevLayer.connection isVideoOrientationSupported]){
            self.prevLayer.connection.videoOrientation = newOrientation;
        }else{
            NSLog(@"NO respond to selector connection");
        }
    }else{


if ([self.prevLayer isOrientationSupported]){
        self.prevLayer.orientation = newOrientation;
    }else{
        NSLog(@"NO isOrientationSupported");
    }

}
于 2013-05-10T10:45:31.077 回答
1

请尝试以下代码的方向,所以我认为您的问题可能会得到解决。

   - (void)encodeVideoOrientation:(NSURL *)anOutputFileURL
    {
    CGAffineTransform rotationTransform;
    CGAffineTransform rotateTranslate;
    CGSize renderSize;

    switch (self.recordingOrientation)
    {
        // set these 3 values based on orientation

    }


    AVURLAsset * videoAsset = [[AVURLAsset alloc]initWithURL:anOutputFileURL options:nil];

    AVAssetTrack *sourceVideoTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
    AVAssetTrack *sourceAudioTrack = [[videoAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];

    AVMutableComposition* composition = [AVMutableComposition composition];

    AVMutableCompositionTrack *compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
    [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration)
                                   ofTrack:sourceVideoTrack
                                    atTime:kCMTimeZero error:nil];
    [compositionVideoTrack setPreferredTransform:sourceVideoTrack.preferredTransform];

    AVMutableCompositionTrack *compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio
                                                                                preferredTrackID:kCMPersistentTrackID_Invalid];
    [compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration)
                                   ofTrack:sourceAudioTrack
                                    atTime:kCMTimeZero error:nil];



    AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
    AVMutableVideoCompositionLayerInstruction *layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:compositionVideoTrack];
    [layerInstruction setTransform:rotateTranslate atTime:kCMTimeZero];

    AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition];
    videoComposition.frameDuration = CMTimeMake(1,30);
    videoComposition.renderScale = 1.0;
    videoComposition.renderSize = renderSize;
    instruction.layerInstructions = [NSArray arrayWithObject: layerInstruction];
    instruction.timeRange = CMTimeRangeMake(kCMTimeZero, videoAsset.duration);
    videoComposition.instructions = [NSArray arrayWithObject: instruction];

    AVAssetExportSession * assetExport = [[AVAssetExportSession alloc] initWithAsset:composition
                                                                          presetName:AVAssetExportPresetMediumQuality];

    NSString* videoName = @"export.mov";
    NSString *exportPath = [NSTemporaryDirectory() stringByAppendingPathComponent:videoName];

    NSURL * exportUrl = [NSURL fileURLWithPath:exportPath];

    if ([[NSFileManager defaultManager] fileExistsAtPath:exportPath])
    {
        [[NSFileManager defaultManager] removeItemAtPath:exportPath error:nil];
    }

    assetExport.outputFileType = AVFileTypeMPEG4;
    assetExport.outputURL = exportUrl;
    assetExport.shouldOptimizeForNetworkUse = YES;
    assetExport.videoComposition = videoComposition;

    [assetExport exportAsynchronouslyWithCompletionHandler:
     ^(void ) {
         switch (assetExport.status)
         {
             case AVAssetExportSessionStatusCompleted:
                 //                export complete
                 NSLog(@"Export Complete");
                 break;
             case AVAssetExportSessionStatusFailed:
                 NSLog(@"Export Failed");
                 NSLog(@"ExportSessionError: %@", [assetExport.error localizedDescription]);
                 //                export error (see exportSession.error)
                 break;
             case AVAssetExportSessionStatusCancelled:
                 NSLog(@"Export Failed");
                 NSLog(@"ExportSessionError: %@", [assetExport.error localizedDescription]);
                 //                export cancelled
                 break;
         }
     }];

    }

希望这对你有帮助。!!

于 2013-05-09T11:53:37.237 回答