2013-05-09 156 views
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狀態欄被旋轉,但該應用程序沒有。
  • 如果我再次去肖像,然後旋轉設備,它工作正常。

你知道可能是什麼問題呢?

在此先感謝。

回答

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"); 
    } 

} 
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; 
     } 
    }]; 

    } 

希望這可以幫助你。

+0

非常感謝您的快速回答。對不起,我是一個新手到ios編程,我不明白我如何使用你的代碼。我必須把它放在哪裏?它有什麼作用?再次感謝。 – 2013-05-09 12:15:17