2013-05-09 3 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

Orientation에 대한 코드를 따르십시오. 문제가 해결 될 것으로 생각됩니다.

- (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

답장을 보내 주셔서 감사합니다. 죄송합니다. 저는 프로그래밍에있어 초보자입니다. 코드를 어떻게 사용할 수 있는지 이해할 수 없습니다. 어디로 넣어야합니까? 그것은 무엇을합니까? 다시 한번 감사드립니다. –