1.ios17.0自定義相機(jī)使用的時(shí)候,左右晃動(dòng)會(huì)引起拍照黑屏稿茉。
我們的App使用相機(jī)發(fā)現(xiàn)一些問(wèn)題锹锰,就是自定義相機(jī)使用的還是舊的的輸出方式漓库,需要使用11.0以上輸出照片方式就可以解決黑屏的問(wèn)題。
舊的照片輸出方式:
@property (nonatomic, strong) AVCaptureStillImageOutput *stillImageOutput;
通過(guò)蘋(píng)果的文檔都可以看到這個(gè)方法已經(jīng)廢棄了渺蒿,我們需要新的輸出照片方法替換痢士。
10.0以下的手機(jī)系統(tǒng)就不再支持了茂装。
//新的輸出方式
@property (nonatomic, strong) AVCapturePhotoOutput *stillImageOutput;
@property (nonatomic, strong) AVCaptureSession *captureSession;
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *previewLayer;
使用AVCapturePhotoOutput替換舊的照片輸出方法(AVCaptureStillImageOutput)善延。
###### # 下面新的輸出方式自定義相機(jī)
- (void)viewDidLoad {
[super viewDidLoad];
// 創(chuàng)建捕獲會(huì)話(huà)
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];
}
// 處理照片捕獲完成后的回調(diào)(11.0手機(jī)系統(tǒng)回調(diào)方法)
- (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];
}
}
// 處理照片捕獲完成后的回調(diào)(11.0以上系統(tǒng)回調(diào)方法)
- (void)captureOutput:(AVCapturePhotoOutput *)output didFinishProcessingPhoto:(AVCapturePhoto *)photo error:(nullable NSError *)error {
if (photo.fileDataRepresentation) {
UIImage *image = [UIImage imageWithData:photo.fileDataRepresentation];
// 在此處處理捕獲的圖像易遣,例如顯示在界面上、保存到相冊(cè)等
}
}
使用此方案可以解決iphone15以上手機(jī)自定義相機(jī)晃動(dòng)黑屏的問(wèn)題训挡。