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

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 に答える