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

还有黑屏问题的话,可以私信我沟通。
相关推荐
安和昂14 分钟前
iOS 工厂模式
ios
龙湾开发1 小时前
轻量级高性能推理引擎MNN 学习笔记 03.在iOS运行MNN的示例
c++·学习·ios·图形渲染·mnn
初遇你时动了情5 小时前
flutter 配置 安卓、Ios启动图
android·flutter·ios
WDeLiang9 小时前
Flutter - UIKit开发相关指南 - 线程和异步
flutter·ios·dart
Unlimitedz18 小时前
iOS音视频解封装分析
ios·音视频
lichao89042719 小时前
谈谈未来iOS越狱或巨魔是否会消失
ios
fzxwl1 天前
隆重推荐(Android 和 iOS)UI 自动化工具—Maestro
android·ui·ios
若水无华3 天前
fiddler 配置ios手机代理调试
ios·智能手机·fiddler
Aress"3 天前
【ios越狱包安装失败?uniapp导出ipa文件如何安装到苹果手机】苹果IOS直接安装IPA文件
ios·uni-app·ipa安装
Jouzzy3 天前
【iOS安全】Dopamine越狱 iPhone X iOS 16.6 (20G75) | 解决Jailbreak failed with error
安全·ios·iphone