我只是想知道这是否可能:
我一直在寻找各种显示相机预览的解决方案;虽然在全屏模式下这样做相对简单,但我想做的是将其缩放到屏幕的 50% 并与图形并排呈现(不是覆盖,而是一个单独的图形)位于相机预览的左侧,占用相同的空间)。基本上目的是让用户将相机预览与图形进行比较。
所以,我需要知道的是:
您可以使用下一个代码:
previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:captureSession];
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
previewLayer.opaque = YES;
previewLayer.contentsScale = self.view.contentScaleFactor;
previewLayer.frame = self.view.bounds;
previewLayer.needsDisplayOnBoundsChange = YES;
[self.view.layer addSublayer:previewLayer];
只需替换第 5 行即可将预览图层设置为另一帧。 您可以使用此代码创建 captureSession
captureSession = [[AVCaptureSession alloc] init];
if(!captureSession)
{
NSLog(@"Failed to create video capture session");
return NO;
}
[captureSession beginConfiguration];
captureSession.sessionPreset = AVCaptureSessionPreset640x480;
AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
videoDevice.position = AVCaptureDevicePositionFront;
if(!videoDevice)
{
NSLog(@"Couldn't create video capture device");
[captureSession release];
captureSession = nil;
return NO;
}
if([videoDevice isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus])
{
NSError *deviceError = nil;
if([videoDevice lockForConfiguration:&deviceError])
{
[videoDevice setFocusMode:AVCaptureFocusModeContinuousAutoFocus];
[videoDevice unlockForConfiguration];
}
else
{
NSLog(@"Couldn't lock device for configuration");
}
}
NSError *error;
AVCaptureDeviceInput *videoIn = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];
if(!videoIn)
{
NSLog(@"Couldn't create video capture device input: %@ - %@", [error localizedDescription], [error localizedFailureReason]);
[captureSession release];
captureSession = nil;
return NO;
}
if(![captureSession canAddInput:videoIn])
{
NSLog(@"Couldn't add video capture device input");
[captureSession release];
captureSession = nil;
return NO;
}
[captureSession addInput:videoIn];
[captureSession commitConfiguration];