Objective-C、iPhone iOS 6.0
以下のコードは縦方向の向きをうまくサポートしていますが、横向きにしようとすると、ピンチして自動ズームすると、ぎこちない縦向きモードに戻ります (横向きビュー)。
@implementation AVCapture
{
CGFloat beginGestureScale;
CGFloat effectiveScale;
}
- (id)initWithView:(UIView *)cameraView
{
......
self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];
[self rotateToInterfaceOrientation:(UIInterfaceOrientation)[UIDevice currentDevice].orientation];
[self.previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
self.previewLayer.frame = cameraView.frame;
[self.previewLayer setPosition:CGPointMake(CGRectGetMidX(cameraView.bounds), CGRectGetMidY(cameraView.bounds))];
[cameraView.layer addSublayer:self.previewLayer];
effectiveScale = 1.0;
......
}
- (void)rotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
CALayer *previewLayer = self.previewLayer;
switch (toInterfaceOrientation)
{
case UIDeviceOrientationLandscapeLeft:
previewLayer.transform = CATransform3DMakeRotation(M_PI+M_PI_2, 0, 0, 1); // 270 degress
break;
case UIDeviceOrientationLandscapeRight:
previewLayer.transform = CATransform3DMakeRotation(M_PI_2, 0, 0, 1); // 270 degress
break;
case UIDeviceOrientationPortraitUpsideDown:
previewLayer.transform = CATransform3DMakeRotation(M_PI, 0, 0, 1); // 270 degress
break;
default:
previewLayer.transform = CATransform3DMakeRotation(0.0, 0, 0, 1); // 270 degress
break;
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#pragma mark Pinch to Zoom Gestures
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if ( [gestureRecognizer isKindOfClass:[UIPinchGestureRecognizer class]] )
beginGestureScale = effectiveScale;
return YES;
}
- (void)handlePinchGesture:(UIPinchGestureRecognizer *)recognizer
{
if ([recognizer numberOfTouches] < 2)
return;
BOOL allTouchesAreOnThePreviewLayer = YES;
NSUInteger numTouches = [recognizer numberOfTouches], i;
for (i=0; i<numTouches; ++i)
{
CGPoint location = [recognizer locationOfTouch:i inView:m_CameraView];
CGPoint convertedLocation = [self.previewLayer convertPoint:location fromLayer:self.previewLayer.superlayer];
if (![self.previewLayer containsPoint:convertedLocation])
{
allTouchesAreOnThePreviewLayer = NO;
break;
}
}
if (allTouchesAreOnThePreviewLayer)
{
effectiveScale = beginGestureScale * recognizer.scale;
if (effectiveScale < 1.0)
effectiveScale = 1.0;
CGFloat maxScaleAndCropFactor = [[self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo] videoMaxScaleAndCropFactor];
if (effectiveScale > maxScaleAndCropFactor)
effectiveScale = maxScaleAndCropFactor;
[CATransaction begin];
[CATransaction setAnimationDuration:.025];
// Scale
[self.previewLayer setAffineTransform:CGAffineTransformMakeScale(effectiveScale, effectiveScale)];
[CATransaction commit];
}
}
変換を維持する方法がわかりません。