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

还有黑屏问题的话,可以私信我沟通。
相关推荐
恋猫de小郭20 小时前
Flutter Widget Preview 功能已合并到 master,提前在体验毛坯的预览支持
android·flutter·ios
点金石游戏出海1 天前
每周资讯 | Krafton斥资750亿日元收购日本动画公司ADK;《崩坏:星穹铁道》新版本首日登顶iOS畅销榜
游戏·ios·业界资讯·apple·崩坏星穹铁道
旷世奇才李先生1 天前
Swift 安装使用教程
开发语言·ios·swift
90后的晨仔1 天前
Xcode16报错: SDK does not contain 'libarclite' at the path '/Applicati
ios
finger244801 天前
谈一谈iOS线程管理
ios·objective-c
Digitally1 天前
如何将大型视频文件从 iPhone 传输到 PC
ios·iphone
梅名智1 天前
IOS 蓝牙连接
macos·ios·cocoa
美狐美颜sdk2 天前
跨平台直播美颜SDK集成实录:Android/iOS如何适配贴纸功能
android·人工智能·ios·架构·音视频·美颜sdk·第三方美颜sdk
恋猫de小郭2 天前
Meta 宣布加入 Kotlin 基金会,将为 Kotlin 和 Android 生态提供全新支持
android·开发语言·ios·kotlin
泓博2 天前
Objective-c把字符解析成字典
开发语言·ios·objective-c