在 iOS 开发中,我们经常会使用扫描二维码/条形码功能,为了提高开发效率和方便大家使用,我自己写一个 DYFCodeScanner 类,先来看看效果:
效果图
Code Scanner Preview DYFCodeScanner
具有二维码和条码扫描功能,有一套自定义的扫描动画以及界面,支持相机缩放,还可以生成和识别二维码,使用起来很方便。
安装
- 支持通过 CocoaPods 安装。
# Install lastest version.
pod 'DYFCodeScanner'
#pod 'DYFCodeScanner', '~> 1.0.1'
接下来一起阅读它的使用说明:
使用说明
- 添加隐私 (Add Privacy)
<key>NSCameraUsageDescription</key>
<string>扫描二维码/条形码,允许App访问您的相机?</string>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>存取照片,允许App访问您的相册?</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>存取照片,允许App访问您的相册?</string>
- 导入头文件 (Import Header)
#import "DYFCodeScanner.h"
- 扫描二维码/条形码 (Scanning QR code / Barcode)
支持push和模态两种场景过渡。 (Supporting push or modal transition.)
根据项目的需求,选择之一即可。(According to the needs of the project, you can choose one of them.)
当你要推进视图控制器,你可以选择隐藏或者不隐藏导航条。(When you want to push the view controller, you can choose to or not to hide the navigation bar.)
- (IBAction)scan:(id)sender {
static BOOL shouldPush = YES;
static BOOL naviBarHidden = YES;
DYFCodeScannerViewController *codesVC = [[DYFCodeScannerViewController alloc] init];
codesVC.scanType = DYFCodeScannerTypeAll;
codesVC.navigationTitle = @"二维码/条形码";
codesVC.tipString = [NSString stringWithFormat:@"将二维码/条形码放入框内,即自动扫描"];
codesVC.resultHandler = ^(BOOL result, NSString *stringValue) {
if (result) {
[self showResult:stringValue];
} else {
NSLog(@"QRCode Message: %@", stringValue);
}
};
if (shouldPush) {
codesVC.navigationBarHidden = naviBarHidden = !naviBarHidden;
[self.navigationController pushViewController:codesVC animated:YES];
} else {
[self presentViewController:codesVC animated:YES completion:NULL];
}
shouldPush = !shouldPush;
}
- (void)showResult:(NSString *)value {
if (self.m_textView.text.length > 0) {
self.m_textView.text = @"";
}
self.m_textView.text = value;
}
- 生成二维码 (Generating QR code)
- 生成带中心图像的二维码 (Generating QR code that contains center image)
- (IBAction)generateQRCode:(id)sender {
CGRect rect = self.qrc_imageView.frame;
DYFQRCodeImageView *imageView = [DYFQRCodeImageView createWithFrame:rect stringValue:@"http://img.shields.io/cocoapods/v/DYFAssistiveTouchView.svg?style=flat" centerImage:[UIImage imageNamed:@"cat49334.jpg"]];
self.qrc_imageView.image = imageView.image;
}
- 生成带颜色的二维码 (Generating QR code that contains backgroudColor and foregroudColor)
- (IBAction)generateColorQRCode:(id)sender {
CGRect rect = self.cqrc_imageView.frame;
DYFQRCodeImageView *imageView = [DYFQRCodeImageView createWithFrame:rect stringValue:@"http://img.shields.io/cocoapods/p/DYFAssistiveTouchView.svg?style=flat" backgroudColor:[UIColor grayColor] foregroudColor:[UIColor greenColor]];
self.cqrc_imageView.image = imageView.image;
}
- 生成带中心图像颜色的二维码 (Generating QR code that contains center image, backgroudColor and foregroudColor)
DYFQRCodeImageView *imageView = [DYFQRCodeImageView createWithFrame:CGRectMake(30, 88, 200, 200) stringValue:@"https://github.com/dgynfi/DYFAuthIDAndGestureLock" backgroudColor:[UIColor grayColor] foregroudColor:[UIColor greenColor] centerImage:[UIImage imageNamed:@"cat49334.jpg"]];
[self.view addSubview:imageView];
- 识别二维码 (Recognizing QR code)
- (void)recognizeQRCode:(UILongPressGestureRecognizer *)recognizer {
UIImage *qrcodeImage = ((DYFQRCodeImageView *)recognizer.view).image;
NSString *stringValue = [qrcodeImage yf_stringValue];
#if DEBUG
NSLog(@"stringValue: %@", stringValue);
#endif
}
Sample Codes
技术交流群(群号:155353383)
- 欢迎加入技术交流群,一起探讨技术问题。
如果你觉得能帮助到你,请去我的 Github 项目给一颗小星星。谢谢!(If you think it can help you, please go to my github project and give it a star. Thanks!)
希望读到这的您能点赞,分享和关注一下我,以后还会更新技术干货,谢谢您的支持!
点赞+关注,第一时间获取最新知识点,转发请注明出处。
最后祝大家生活愉快~
网友评论