From 2f4ec7c1372351b411b3ce010cb1e3b8ccfee3b1 Mon Sep 17 00:00:00 2001 From: jeremylu <1509028992@qq.com> Date: Fri, 5 Jan 2024 14:21:42 +0800 Subject: [PATCH 1/3] fix bug: 1.Continuous autofocus not working on iPhone 14? https://forums.developer.apple.com/forums/thread/719885 2.AVCaptureSession startRunning This call blocks until the session object has completely started up or failed --- SGQRCode/QRCode/SGScanCode.m | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/SGQRCode/QRCode/SGScanCode.m b/SGQRCode/QRCode/SGScanCode.m index d4a4063..0fe721b 100644 --- a/SGQRCode/QRCode/SGScanCode.m +++ b/SGQRCode/QRCode/SGScanCode.m @@ -23,6 +23,8 @@ @interface SGScanCode () *devices = discoverySession.devices; + // 选择其中一个摄像头设备,例如第一个 + if (devices.count > 0) { + AVCaptureDevice *selectedDevice = devices.firstObject; + _device = selectedDevice; + } + } } return _device; } From d7b36cd8936abc33c971300e6577799f06b290ed Mon Sep 17 00:00:00 2001 From: jeremylu <1509028992@qq.com> Date: Thu, 11 Jan 2024 17:27:58 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=A4=84=E7=90=86ipad=E4=B8=8A=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2=E6=98=BE=E7=A4=BA=E4=B8=8D=E6=AD=A3=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SGQRCode/QRCode/SGScanCode.m | 19 ++++++++++++++++--- SGQRCode/ScanView/SGScanView.m | 6 ++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/SGQRCode/QRCode/SGScanCode.m b/SGQRCode/QRCode/SGScanCode.m index 0fe721b..532d105 100644 --- a/SGQRCode/QRCode/SGScanCode.m +++ b/SGQRCode/QRCode/SGScanCode.m @@ -55,8 +55,9 @@ - (instancetype)init { #pragma mark - - .h公开的属性 - (void)setPreview:(UIView *)preview { - _preview = preview; - [preview.layer insertSublayer:self.videoPreviewLayer atIndex:0]; + _preview = preview; + [preview.layer insertSublayer:self.videoPreviewLayer atIndex:0]; + [self addEventObserver]; } - (void)setDelegate:(id)delegate { @@ -153,6 +154,19 @@ - (void)playSoundEffect:(NSString *)name { [soundEffect play]; } +#pragma mark - 屏幕旋转 +- (void)addEventObserver{ + __weak typeof(self) weakSelf = self; + void(^result)(NSNotification* ) = ^(NSNotification* notification){ + if(nil == weakSelf){return;} + AVCaptureConnection* connection = weakSelf.videoPreviewLayer.connection; + if(NO == connection.isVideoOrientationSupported){return;} + [connection setVideoOrientation:[[UIApplication sharedApplication] statusBarOrientation]]; + weakSelf.videoPreviewLayer.frame = weakSelf.preview.bounds; + }; + [[NSNotificationCenter defaultCenter]addObserverForName:@"SGScanViewBoundUpdate" object:nil queue:[NSOperationQueue mainQueue] usingBlock:result]; + [[NSNotificationCenter defaultCenter]addObserverForName:UIApplicationDidChangeStatusBarOrientationNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:result]; +} #pragma mark - - 内部属性 - (AVCaptureSession *)session { @@ -210,7 +224,6 @@ - (AVCaptureVideoPreviewLayer *)videoPreviewLayer { if (!_videoPreviewLayer) { _videoPreviewLayer = [AVCaptureVideoPreviewLayer layerWithSession:_session]; _videoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; - _videoPreviewLayer.frame = self.preview.frame; } return _videoPreviewLayer; } diff --git a/SGQRCode/ScanView/SGScanView.m b/SGQRCode/ScanView/SGScanView.m index 9a95d01..34d3c04 100644 --- a/SGQRCode/ScanView/SGScanView.m +++ b/SGQRCode/ScanView/SGScanView.m @@ -335,4 +335,10 @@ - (void)updateUI { } } + +- (void)layoutSubviews{ + [super layoutSubviews]; + [[NSNotificationCenter defaultCenter]postNotificationName:@"SGScanViewBoundUpdate" object:NSStringFromCGRect(self.bounds)]; +} + @end From a3054dd68f94e46902a82291b897f50a31ed836a Mon Sep 17 00:00:00 2001 From: jeremylu <1509028992@qq.com> Date: Fri, 2 Feb 2024 15:15:42 +0800 Subject: [PATCH 3/3] =?UTF-8?q?fix=20=20bug:=201.=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E5=A4=9A=E9=95=9C=E5=A4=B4=E6=89=8B=E6=9C=BA=EF=BC=8C=E9=97=AA?= =?UTF-8?q?=E5=85=89=E7=81=AF=E6=89=93=E5=BC=80bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SGQRCode/QRCode/SGScanCode.h | 5 +++++ SGQRCode/QRCode/SGScanCode.m | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/SGQRCode/QRCode/SGScanCode.h b/SGQRCode/QRCode/SGScanCode.h index 7b4f94c..5d3a7c1 100644 --- a/SGQRCode/QRCode/SGScanCode.h +++ b/SGQRCode/QRCode/SGScanCode.h @@ -46,4 +46,9 @@ /// 播放音效 - (void)playSoundEffect:(NSString *)name; +///打开手电筒 +- (void)turnOnTorch; + +- (void)turnOffTorch; + @end diff --git a/SGQRCode/QRCode/SGScanCode.m b/SGQRCode/QRCode/SGScanCode.m index 532d105..d31ae7d 100644 --- a/SGQRCode/QRCode/SGScanCode.m +++ b/SGQRCode/QRCode/SGScanCode.m @@ -154,6 +154,28 @@ - (void)playSoundEffect:(NSString *)name { [soundEffect play]; } + +- (void)turnOnTorch { + AVCaptureDevice *device = self.device; + if ([device hasTorch]) { + BOOL locked = [device lockForConfiguration:nil]; + if (locked) { + [device setTorchMode:AVCaptureTorchModeOn]; + [device unlockForConfiguration]; + } + } +} + +- (void)turnOffTorch { + AVCaptureDevice *device = self.device; + if ([device hasTorch]) { + [device lockForConfiguration:nil]; + [device setTorchMode:AVCaptureTorchModeOff]; + [device unlockForConfiguration]; + } +} + + #pragma mark - 屏幕旋转 - (void)addEventObserver{ __weak typeof(self) weakSelf = self;