iphoneioscamera-overlay

How to handle the change of orientation like the standard camera application on the iPhone


I use AVFoundation framework to get access to camera. In the standard camera application, when the orientation changes only the rotation of the icons on the buttons. But when I try to do something like, I have a problem related to the orientation changes animation. Preview view is rotated and the area behind it is black and has image jerks when turning.

Now I have this code:

-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

    [CATransaction begin];
    [CATransaction setValue:[NSNumber numberWithFloat:duration]
                     forKey:kCATransactionAnimationDuration];
    if (toInterfaceOrientation==UIInterfaceOrientationLandscapeLeft){
        captureManager.previewLayer.transform=CATransform3DMakeRotation(M_PI/2, 0, 0, 1.0);
        captureManager.previewLayer.frame = CGRectMake(0, 0, 480, 320);
    } else if (toInterfaceOrientation==UIInterfaceOrientationPortrait){
        captureManager.previewLayer.transform=CATransform3DMakeRotation(0, 0, 0, 1.0);
        captureManager.previewLayer.frame = CGRectMake(0, 0, 320, 480);
    } else if (toInterfaceOrientation==UIInterfaceOrientationLandscapeRight){
        captureManager.previewLayer.transform=CATransform3DMakeRotation(M_PI+M_PI/2, 0, 0, 1.0);
        captureManager.previewLayer.frame = CGRectMake(0, 0, 480, 320);
    }

    [CATransaction commit];
}

My idea it is use transform animation to deal with rotation animation but I get problems with glithes.

How can create camera preview view and solve problems with orientation change? May be I must use other instrument to work with camera? Not AVFoundation framework?

I know that I can lock orientation and use accelerometer to deal with buttons icons. But the i get problems with some subviewcontrollers.

Sorry for terrible english.


Solution

  • You need to disable landscape orientation for your controller and update layout of icons etc by handling orientation change notifications. In this topic it is described right - How to handle autorotation in AVCaptureVideoPreviewLayer?