美文网首页
ios17.0自定义相机拍照黑屏的解决方案

ios17.0自定义相机拍照黑屏的解决方案

作者: xing_x | 来源:发表于2023-10-25 16:03 被阅读0次

    1.ios17.0自定义相机使用的时候,左右晃动会引起拍照黑屏。

    我们的App使用相机发现一些问题,就是自定义相机使用的还是旧的的输出方式,需要使用11.0以上输出照片方式就可以解决黑屏的问题。

    旧的照片输出方式:

    @property (nonatomic, strong) AVCaptureStillImageOutput *stillImageOutput;
    
    

    通过苹果的文档都可以看到这个方法已经废弃了,我们需要新的输出照片方法替换。
    10.0以下的手机系统就不再支持了。

    //新的输出方式
    @property (nonatomic, strong) AVCapturePhotoOutput *stillImageOutput;
    
    @property (nonatomic, strong) AVCaptureSession *captureSession;
    
    @property (nonatomic, strong) AVCaptureVideoPreviewLayer *previewLayer;
    
    

    使用AVCapturePhotoOutput替换旧的照片输出方法(AVCaptureStillImageOutput)。

    ###### # 下面新的输出方式自定义相机
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        // 创建捕获会话
        self.captureSession = [[AVCaptureSession alloc] init];
        self.captureSession.sessionPreset = AVCaptureSessionPresetPhoto;
        
        // 获取后置摄像头
        AVCaptureDevice *backCamera = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
        if (backCamera) {
            NSError *error = nil;
            AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:backCamera error:&error];
            if (!error && [self.captureSession canAddInput:input]) {
                [self.captureSession addInput:input];
                
                self.stillImageOutput = [[AVCapturePhotoOutput alloc] init];
                if ([self.captureSession canAddOutput:self.stillImageOutput]) {
                    [self.captureSession addOutput:self.stillImageOutput];
                    
                    self.previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession];
                    self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspect;
                    self.previewLayer.connection.videoOrientation = AVCaptureVideoOrientationPortrait;
                    [self.view.layer addSublayer:self.previewLayer];
                    
                    [self.captureSession startRunning];
                }
            } else {
                NSLog(@"Error setting up camera input: %@", [error localizedDescription]);
            }
        }
    }
    - (void)viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];
        self.previewLayer.frame = self.view.bounds;
    }
    
    - (IBAction)capturePhoto:(UIButton *)sender {
       AVCaptureVideoOrientation videoPreviewLayerVideoOrientation = _previewLayer.connection.videoOrientation;
        AVCaptureConnection* photoOutputConnection = [self.photoOutput connectionWithMediaType:AVMediaTypeVideo];
        photoOutputConnection.videoOrientation = videoPreviewLayerVideoOrientation;
        AVCapturePhotoSettings  *photoSettings = [AVCapturePhotoSettings photoSettings];
        AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
        photoSettings.flashMode = device.flashMode;
        [self.photoOutput capturePhotoWithSettings:photoSettings delegate:self];
    }
    // 处理照片捕获完成后的回调(11.0手机系统回调方法)
    - (void)captureOutput:(AVCapturePhotoOutput *)captureOutput didFinishProcessingPhotoSampleBuffer:(nullable CMSampleBufferRef)photoSampleBuffer previewPhotoSampleBuffer:(nullable CMSampleBufferRef)previewPhotoSampleBuffer resolvedSettings:(AVCaptureResolvedPhotoSettings *)resolvedSettings bracketSettings:(nullable AVCaptureBracketedStillImageSettings *)bracketSettings error:(nullable NSError *)error {
        if (error) {
                LogError(@"captureOutput capture still image error.%@",error);
            } else if (photoSampleBuffer) {
                NSData *imageData = [AVCapturePhotoOutput JPEGPhotoDataRepresentationForJPEGSampleBuffer:photoSampleBuffer previewPhotoSampleBuffer:previewPhotoSampleBuffer];
                UIImage *image = [UIImage imageWithData:imageData];
                
            }
     }
    // 处理照片捕获完成后的回调(11.0以上系统回调方法)
    - (void)captureOutput:(AVCapturePhotoOutput *)output didFinishProcessingPhoto:(AVCapturePhoto *)photo error:(nullable NSError *)error {
        if (photo.fileDataRepresentation) {
            UIImage *image = [UIImage imageWithData:photo.fileDataRepresentation];
            // 在此处处理捕获的图像,例如显示在界面上、保存到相册等
        }
    }
    

    使用此方案可以解决iphone15以上手机自定义相机晃动黑屏的问题。

    还有黑屏问题的话,可以私信我沟通。

    相关文章

      网友评论

          本文标题:ios17.0自定义相机拍照黑屏的解决方案

          本文链接:https://www.haomeiwen.com/subject/ybbeidtx.html