IOS 开发学习十九 二维码扫描-QRCodeReaderViewController

一 下载

项目地址:

https://github.com/yannickl/QRCodeReaderViewController

官方介绍的QRCode可以指定编码,特别是它支持ios7+,可以用来替换zxing、zbar。我在项目里一开始使用了zbar,一般时候它是正常的,但是扫描一个很小的二维码时,无法识别。切换为QRCode后,识别效果比较良好。

它会提供一个默认的view controller调用摄像头,同时会提供一个切换前置摄像头和后置摄像头的按钮。

这是官方截屏:


安装

推荐的安装方式是使用CocoaPods 包管理工具。
在Podfile文件里输入:
$ cd /path/to/MyProject
$ touch Podfile
$ vim Podfile
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '7.0'
pod 'QRCodeReaderViewController', '~> 3.4.0'
然后在命令行执行:
$ pod install

等待安装结束后,输入
open ****.xcworkspace 
打开项目。

使用

头文件里:
//
//  ViewController.h
//  smarthome
//
//  Created by 谢厂节 on 15/5/14.
//  Copyright (c) 2015年 WHR. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "QRCodeReaderViewController.h"

@interface ViewController : UIViewController<QRCodeReaderDelegate>

@end


.m文件里:
#import "QRCodeReaderViewController.h"

-(void)actionScan{
    NSArray *types = @[AVMetadataObjectTypeQRCode];
    QRCodeReaderViewController* _reader        = [QRCodeReaderViewController readerWithMetadataObjectTypes:types];
    
    // Set the presentation style
    _reader.modalPresentationStyle = UIModalPresentationFormSheet;
    
    // Using delegate methods
    _reader.delegate = self;
    
    // Or by using blocks
    [_reader setCompletionWithBlock:^(NSString *resultAsString) {
        [self dismissViewControllerAnimated:YES completion:^{
            NSLog(@"%@", resultAsString);
        }];
    }];
    
    [self presentViewController:_reader animated:YES completion:NULL];
}
#pragma mark - QRCodeReader Delegate Methods

- (void)reader:(QRCodeReaderViewController *)reader didScanResult:(NSString *)result
{
    [self dismissViewControllerAnimated:YES completion:^{
        NSLog(@"%@", result);
    }];
}

- (void)readerDidCancel:(QRCodeReaderViewController *)reader
{
    [self dismissViewControllerAnimated:YES completion:NULL];
}
使用CocoaPods安装时,可能编译会有错误,需要把
Pods的Target修改一下设置:
这有另一种解决方式,我没有细看。放在这里作参考:
http://cameronspickert.com/2014/01/20/remove-the-arm64-architecture-from-cocoapods-targets.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程圈子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值