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以上手机自定义相机晃动黑屏的问题。

还有黑屏问题的话,可以私信我沟通。
相关推荐
Memory沙漏7 小时前
IOS如何免费申请开发者证书(uniapp开发)
ios·uni-app
StarkCoder14 小时前
打造炫酷浮动式 TabBar:让 iOS 应用导航更有格调!
前端·ios
2501_9400940216 小时前
索尼PSP游戏资源下载 推荐中文汉化ios格式合集分享开源掌机模拟器都支持
游戏·ios·cocoa
2501_9160074720 小时前
iOS性能调试工具终极指南,从系统底层到多端协同的全方位优化实践(2025版)
android·ios·小程序·https·uni-app·iphone·webview
私人珍藏库20 小时前
Miraplay – iOS端类TVbox可添加解析源的影视聚合播放器+解析影视源
ios·应用·tv·影视
2501_9159214320 小时前
iOS崩溃日志深度分析与工具组合实战,从符号化到自动化诊断的完整体系
android·ios·小程序·uni-app·自动化·cocoa·iphone
2501_916008891 天前
没有源码如何加密 IPA 实战流程与多工具组合落地指南
android·ios·小程序·https·uni-app·iphone·webview
CocoaKier1 天前
微信与苹果就小程序支付达成和解,iOS用户有望在小程序内直接使用苹果支付
ios·apple
QuantumLeap丶1 天前
《uni-app跨平台开发完全指南》- 07 - 数据绑定与事件处理
vue.js·ios·uni-app