AVCaptureVideoPreviewLayer旋转方向

时间:2013-06-19 15:46:44

标签: iphone ios avcapturesession

简单的问题,当iPhone从纵向模式旋转到横向模式时,如何更改AVCaptureVideoPreviewLayer方向,反之亦然。

我知道这个问题已被多次询问过,我不是第一个碰到它的人,但我已经尝试了所有可用的解决方案,我可以谷歌或找到它但仍然无效。

我实现设置AVCapture的代码是。如您所见,我正在监控方向变化

-(IBAction)InitiateCamera
{

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];


   //APPLE CODE
    NSError *error = nil;

    // Create the session
    session = [[AVCaptureSession alloc] init];

    // Configure the session to produce lower resolution video frames, if your
    // processing algorithm can cope. We'll specify medium quality for the
    // chosen device.
    session.sessionPreset = AVCaptureSessionPresetLow;

    // Find a suitable AVCaptureDevice
    device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    // Create a device input with the device and add it to the session.
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device
                                                                        error:&error];
    if (!input) {
        // Handling the error appropriately.
    }
    [session addInput:input];

    // Create a VideoDataOutput and add it to the session
    AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
    [session addOutput:output];

    // Configure your output.
    dispatch_queue_t queue = dispatch_queue_create("myQueue", NULL);
    [output setSampleBufferDelegate:self queue:queue];
    dispatch_release(queue);

    // Specify the pixel format
    output.videoSettings =
    [NSDictionary dictionaryWithObject:
     [NSNumber numberWithInt:kCVPixelFormatType_32BGRA]
                                forKey:(id)kCVPixelBufferPixelFormatTypeKey];


    // If you wish to cap the frame rate to a known value, such as 15 fps, set
    // minFrameDuration.
    output.minFrameDuration = CMTimeMake(1, 15);


    avLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];

    CGRect bounds=self.view.layer.bounds;
    avLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    avLayer.bounds=bounds;
    avLayer.position=CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));

    //avLayer.backgroundColor = [[UIColor blackColor] CGColor];
    [self.view.layer insertSublayer:avLayer atIndex:1];


    // Start the session running to start the flow of data
    [session startRunning];

    // Assign session to an ivar.
    [self setSession:session];

}

我使用这篇文章作为尝试更改AVCapture视图方向的答案 iOS CAlayer Orientation AVCaptureVideoPreviewLayer doesn't rotate

- (void) orientationChanged:(NSNotification *)notification
{
    [self rotateLayer];
}

-(void)rotateLayer
{
    CGRect layerRect = [[[self view] layer] bounds];

    UIDeviceOrientation orientation =[[UIDevice currentDevice]orientation];

    switch (orientation) {
        case UIDeviceOrientationLandscapeLeft:
            avLayer.affineTransform = CGAffineTransformMakeRotation(M_PI+ M_PI_2); // 270 degress

            break;
        case UIDeviceOrientationLandscapeRight:
            avLayer.affineTransform = CGAffineTransformMakeRotation(M_PI_2); // 90 degrees
            break;
        case UIDeviceOrientationPortraitUpsideDown:
            avLayer.affineTransform = CGAffineTransformMakeRotation(M_PI); // 180 degrees
            break;
        default:
            avLayer.affineTransform = CGAffineTransformMakeRotation(0.0);
            [avLayer setBounds:layerRect];
            break;
    }
    [avLayer setPosition:CGPointMake(CGRectGetMidX(layerRect),CGRectGetMidY(layerRect))];

}

问题是,你可以看到我的截图,最初所有内容都显示正确,但在旋转设备后,AVCapture视图全部被淘汰。关于我在rotateLayer中做错了什么的任何建议?

enter image description here enter image description here enter image description here

0 个答案:

没有答案