gpt4 book ai didi

iOS相机应用程序,从前置摄像头切换到后置摄像头时闪烁

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:20:10 25 4
gpt4 key购买 nike

我正在尝试模仿相机应用程序从前置摄像头到后置摄像头的转换。它使用 UIViewAnimationOptionTransitionFlipFromLeft ..

我目前的方法是用前置摄像头拍照,切换到后置摄像头,用后置摄像头拍照,将它们加载到两个 UIImageViews 并使用 ImageView 执行转换..(隐藏当前相机流过程中)..

问题是当我调用从前置摄像头切换到后置摄像头时,cameraView(一个 AVCapturePreviewLayer)会在我隐藏它之前立即切换 View (这发生在最后一行的 flipCameraToFront 中.. [_captureSession commitConfiguration]) ...

希望这是有道理的,这里是我省略的一些代码,如果您认为这对我有帮助,请告诉我,我会发布其余部分。
任何帮助将不胜感激,谢谢

//class:RecordViewController
//iVars for this class..
RecordVideoHandler *_recordHandler;
UIImage *_firstImageForFlip;
UIImage *_secondImageForFlip;
UIImageView *_firstImageViewForFlip;
BOOL isUsingFrontInput;
AVCaptureVideoPreviewLayer *_capturePreviewLayer;
UIView *_capturePreviewView;

- (void)doCameraFlip:(UIButton *)sender
{
[_recordHandler addStillImageOutput];
//This method calls imageReturned:(UIImage *)inImage;
[_recordHandler captureAndReturnStillImage];


}
- (void)imageReturned:(UIImage *)inImage{

if(_firstImageForFlip == nil){
[_capturePreviewLayer setFrame:CGRectZero];
_firstImageForFlip = inImage;
_firstImageViewForFlip = [[UIImageView alloc] initWithImage:_firstImageForFlip];


if(isUsingFrontInput)
[_firstImageViewForFlip setTransform:CGAffineTransformMakeScale(-1.0, 1.0)];


[_firstImageViewForFlip setFrame:_capturePreviewView.bounds];
[_capturePreviewView addSubview:_firstImageViewForFlip];


if(isUsingFrontInput){
[_recordHandler flipCameraToBack];
}else{
[_recordHandler flipCameraToFront];
}
[_recordHandler captureAndReturnStillImage];
}else{
_secondImageForFlip = inImage;
[_recordHandler removeStillImageOutput];
[self finishCameraFlip];
}
}
- (void)finishCameraFlip{
UIImageView *secondImageView = [[UIImageView alloc] initWithImage:_secondImageForFlip];
[secondImageView setFrame:_capturePreviewView.bounds];

if(!isUsingFrontInput)
[secondImageView setTransform:CGAffineTransformMakeScale(-1.0, 1.0)];

[UIView transitionWithView:_capturePreviewView duration:3.3f
options:UIViewAnimationOptionTransitionFlipFromLeft animations:
^{
[_firstImageViewForFlip removeFromSuperview];
[_capturePreviewLayer setFrame:_capturePreviewView.bounds];
[_capturePreviewView addSubview:secondImageView];

} completion:
^(BOOL finished){
[secondImageView removeFromSuperview];
}];

isUsingFrontInput = isUsingFrontInput ? NO : YES;

_firstImageForFlip = nil;
_secondImageForFlip = nil;
}

类:RecordVideoHandler

    //iVars for this class..    
AVCaptureSession *_captureSession;

- (void)flipCameraToFront
{
[_captureSession beginConfiguration];
for (AVCaptureInput *input in _captureSession.inputs) {
if (input == _captureRearInput) {
[_captureSession removeInput:_captureRearInput];
[_captureSession addInput:_captureFrontInput];
}
}
_currentCaptureDevice = _captureDeviceFrontFacing;
[_captureSession commitConfiguration];
}

最佳答案

如果有人想知道,我想通了。我不得不使用 performSelectors 来等待下一个运行循环,因为相机立即翻转,而 View 内容在运行循环之后等待等等……不确定具体细节,这是代码。

- (void)doCameraFlip:(UIButton *)sender
{
NSLog(@"doCameraFlip");
[_recordHandler addStillImageOutput];
[_recordHandler captureAndReturnStillImage];
}
- (void)imageReturned:(UIImage *)inImage{
[_recordHandler removeStillImageOutput];
_imageViewForFlip= [[UIImageView alloc] initWithImage:inImage];
[_imageViewForFlip setFrame:_containerView.bounds];
[_containerView addSubview:_imageViewForFlip];


[self performSelector:@selector(cfe) withObject:nil afterDelay:0.0f];
[self performSelector:@selector(cfe2) withObject:nil afterDelay:0.0f];

}
- (void)cfe{
if(isUsingFrontInput){
[_recordHandler flipCameraToBack];
}else{
[_recordHandler flipCameraToFront];
}

}
- (void)cfe2{
[UIView transitionWithView:_containerView duration:3.0f options:UIViewAnimationOptionTransitionFlipFromRight animations:^{
[_imageViewForFlip removeFromSuperview];
[_containerView addSubview:_capturePreviewView];
} completion:nil];
}

关于iOS相机应用程序,从前置摄像头切换到后置摄像头时闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9058551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com