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

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

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

旧的照片输出方式:

objectivec 复制代码
@property (nonatomic, strong) AVCaptureStillImageOutput *stillImageOutput;

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

objectivec 复制代码
//新的输出方式
@property (nonatomic, strong) AVCapturePhotoOutput *stillImageOutput;

@property (nonatomic, strong) AVCaptureSession *captureSession;

@property (nonatomic, strong) AVCaptureVideoPreviewLayer *previewLayer;

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

###### # 下面新的输出方式自定义相机
ini 复制代码
- (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以上手机自定义相机晃动黑屏的问题。

还有黑屏问题的话,可以私信我沟通。
相关推荐
终端安全笔记12 小时前
iOS 27 给了租赁一个新工具,但它只认受监督的设备
android·网络·安全·ios
大熊猫侯佩13 小时前
独立 App 首发完成 iPhone Duo 展开态适配
ios·swiftui·swift
光影少年13 小时前
setImmediate 和 setTimeout(0) 的区别
android·前端·react.js·ios·前端框架
2501_9160088914 小时前
SSE 和 gRPC 流式接口如何抓包,调试 SSE 与 gRPC 流式接口
网络协议·计算机网络·网络安全·ios·adb·https·udp
HouWan1 天前
Flutter: MediaQuery.of(context) 为什么可能拖慢页面?
android·flutter·ios
用户38034165882971 天前
Broadcast Extension 里的 Live Activity:一次跨进程妥协
ios·音视频开发
匠测AI说1 天前
XCUITest 自动化全解:同进程白盒、XCTest 架构、自动同步机制与 iOS 端选型指南
测试工具·ios·自动化
silianpan1 天前
CAD 文档预览 UTS 插件
android·ios·harmonyos
世纪末の魔术师2 天前
从 Healthory 到 Nature Aimanic:两款 iOS App 审核实战后,我总结了这份过审清单
ios·ios过审·苹果过审·ios应用过审