最近因为项目中有扫码登录的需求,一开始用的是系统的AVFoundation库实现的扫码功能
@property (nonatomic,strong) AVCaptureSession *captureSession;//捕捉会话
@property (nonatomic,strong) AVCaptureDeviceInput *deviceInput;//输入流
@property (nonatomic,strong) AVCaptureMetadataOutput *metaDataOutput;//输出流
@property (nonatomic,strong) AVCaptureVideoPreviewLayer *videoPreviewLayer;//预览图层
//开始扫描
- (void)startCapture{
if (![self requestDeviceAuthorization]) {
[[UUCommon shareInstance] uu_alertWithTitle:@"没有相机访问权限" message:@"请在设置->通用->相机中允许访问" centerStr:@"去设置" centerItem:^{
NSURL *url = [[NSURL alloc] initWithString:UIApplicationOpenSettingsURLString];// 跳转至系统设置
[[UIApplication sharedApplication] openURL:url];
}];
return;
}
[self.captureSession beginConfiguration];
if ([self.captureSession canAddInput:self.deviceInput]) {
[self.captureSession addInput:self.deviceInput];
}
// 设置数据输出类型,需要将数据输出添加到会话后,才能指定元数据类型,否则会报错
if ([self.captureSession canAddOutput:self.metaDataOutput]) {
[self.captureSession addOutput:self.metaDataOutput];
//设置扫码支持的编码格式(如下设置条形码和二维码兼容)
NSArray *types = @[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code,AVMetadataObjectTypeCode93Code];
self.metaDataOutput.metadataObjectTypes =types;
}
[self.captureSession commitConfiguration];
[self.captureSession startRunning];
}
//停止扫描
- (void)stopCapture{
[self.captureSession stopRunning];
[self.shadowView uu_scanStop];
}
#pragma mark - 请求权限
- (BOOL)requestDeviceAuthorization{
AVAuthorizationStatus deviceStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (deviceStatus == AVAuthorizationStatusRestricted ||
deviceStatus ==AVAuthorizationStatusDenied){
return NO;
}
return YES;
}
#pragma mark - AVCaptureMetadataOutputObjectsDelegate
- (void)captureOutput:(AVCaptureOutput *)output didOutputMetadataObjects:(NSArray<__kindof AVMetadataObject *> *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
//获取到扫描的数据
AVMetadataMachineReadableCodeObject *dateObject = (AVMetadataMachineReadableCodeObject *) [metadataObjects lastObject];
[self.captureSession stopRunning];
NSLog("%@", dateObject.stringValue);
}
后来参考一下第三方的ZXing库,可以解决系统扫码识别乱码问题,主要是和安卓端保持一致,就开始修改成ZXing的扫码:
//导入头文件
#import
//遵循代理
//ZXCapture捕获器
@property (nonatomic, strong) ZXCapture *capture; //ZXCapture捕获器
//加载ZXCapture捕获器
- (void)setupCapture {
self.capture = [[ZXCapture alloc] init];
self.capture.camera = self.capture.back;
//自动聚焦
self.capture.focusMode = AVCaptureFocusModeContinuousAutoFocus;
[self.view.layer addSublayer:self.capture.layer];
self.capture.delegate = self;
}
//开始扫描
- (void)startCapture {
if (![self requestDeviceAuthorization]) {
[self alertWithTitle:@"没有相机权限" message:@"请在设置-通用-相机-允许访问" leftStr:@"去设置" rightStr:@"取消" leftItem:^{
NSURL *openURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:openURL];
} rightItem:^{
}];
return;
}
//TODO:ZXing捕获器
[self.capture start];
}
//停止扫描
- (void)stopCapture {
//TODO:ZXing停止扫码
[self.capture stop];
}
#pragma mark - ZXCaptureDelegate
- (void)captureResult:(ZXCapture *)capture result:(ZXResult *)result {
if (!result) return;
if (self.capture.layer.superlayer) {
if (result.barcodeFormat == kBarcodeFormatQRCode) {
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
[self stopCapture];
NSLog(@"识别的二维码的内容 = %@", result.text);
}
}
}
这就完了?等等,扫码的功能是实现了,本例中用到的是模态出的扫描二维码页面,dismiss的时候在iOS13的机型上会闪退。通过百度大法后才知道ZXing库需要在控制器dismiss的前移除layer,不然会导致崩溃。
修复的代码:
self.capture.delegate = nil;
[self.capture.layer removeFromSuperlayer];