前言: 使用CIDetector這個類只能識別二維碼圖片,條形碼目前暫不支持bash
能夠經過使用ZBarSDK第三方來識別相冊中的二維碼或條形碼ui
pod 'ZBarSDK', '~> 1.3.1'
複製代碼
//打開相冊
- (void)openPhoto {
//ZBarsSDK初始化
ZBarReaderController *imagePicker = ZBarReaderController.new;
imagePicker.showsHelpOnFail = NO;//禁止顯示讀取失敗頁面
imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;
[self presentViewController:imagePicker animated:YES completion:nil];
}
//選中圖片後的處理
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey, id> *)info {
id<NSFastEnumeration> results = [info objectForKey:ZBarReaderControllerResults];
ZBarSymbol *symbol = nil;
for (symbol in results) {
break;
}
[picker dismissViewControllerAnimated:YES completion:^{
//掃碼信息
NSString *QRCodeString = symbol.data;
NSLog(@"QRCodeString = %@",QRCodeString);
}];
}
備註:若是想經過ZBarSDK來進行拍照識別二維碼和條形碼也能夠,只需將 - (void)openPhoto;中的sourceType改成:
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
便可, 可是注意:這種方式是拍照識別,不是掃一掃識別;
複製代碼