我一直在使用AVFoundation
从相机预览。以下是我如何设置它:
// create session
_session = [[AVCaptureSession alloc] init];
_session.sessionPreset = AVCaptureSessionPresetPhoto;
// configure device
AVCaptureDevice *device;
NSArray *devices = [AVCaptureDevice devices];
for (AVCaptureDevice *singleDevice in devices) {
if ([singleDevice hasMediaType:AVMediaTypeVideo]) {
if ([singleDevice position] == AVCaptureDevicePositionFront) {
device = singleDevice;
}
}
}
NSLog(@"Supported frame rates: %@",[device activeFormat].videoSupportedFrameRateRanges);
AVFrameRateRange* frameRange = [[device activeFormat].videoSupportedFrameRateRanges firstObject];
[device lockForConfiguration:nil];
[device setActiveVideoMinFrameDuration:frameRange.minFrameDuration];
[device setActiveVideoMaxFrameDuration:frameRange.maxFrameDuration];
[device unlockForConfiguration];
NSError *error = nil;
AVCaptureDeviceInput *deviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:device error:&error];
if (deviceInput) {
[_session addInput:deviceInput];
}
else {
NSLog(@"Some wierd shit happened");
return;
}
// set preview layer (camera output)
_previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:_session];
_previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
_previewLayer.frame = CGRectMake(0, 0, self.cameraView.frame.size.width, self.cameraView.frame.size.height);
_previewLayer.connection.videoOrientation = [self videoOrientationFromCurrentDeviceOrientation];
[self.cameraView.layer addSublayer:_previewLayer];
好的工作。现在,在相机视图上方,我有覆盖视图UILabel
。当我想在代码中更改该标签的文本时,它会发生变化但是一秒钟,预览图层会闪烁,就像它冻结一瞬间一样。我不知道为什么会这样,有人对此有更多经验吗?