3

简单的问题,当 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 不旋转的答案

- (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中做错了什么有什么建议吗?

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

4

0 回答 0