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

还有黑屏问题的话,可以私信我沟通。
相关推荐
AirDroid_cn几秒前
OPPO手机怎样被其他手机远程控制?两台OPPO手机如何相互远程控制?
android·windows·ios·智能手机·iphone·远程工作·远程控制
杂雾无尘2 小时前
开发者必看,全面解析应用更新策略,让用户无法拒绝你的应用更新!
ios·xcode·swift
xiangzhihong83 小时前
使用Universal Links与Android App Links实现网页无缝跳转至应用
android·ios
Digitally5 小时前
如何将iPhone备份到Mac/MacBook
macos·ios·iphone
帅次5 小时前
【iOS设计模式】深入理解MVC架构 - 重构你的第一个App
ios·swiftui·objective-c·iphone·swift·safari·cocoapods
Frank学习路上21 小时前
【IOS】XCode创建firstapp并运行(成为IOS开发者)
开发语言·学习·ios·cocoa·xcode
瓜子三百克1 天前
CALayer的异步处理
macos·ios·cocoa
吴Wu涛涛涛涛涛Tao1 天前
一步到位:用 Very Good CLI × Bloc × go_router 打好 Flutter 工程地基
flutter·ios
杂雾无尘1 天前
开发者必看:如何在 iOS 应用中完美实现动态自定义字体!
ios·swift·apple
kymjs张涛1 天前
零一开源|前沿技术周报 #6
前端·ios·harmonyos