使用系统提供的AVFoundation 实现二维码扫描(带动画)

注意这里的扫描必须真机测试

模拟器上是看不出来效果的

#import "ViewController.h"
#import 

@interface ViewController ()

//输入和输出设备的桥梁
@property (nonatomic, strong) AVCaptureSession *session;

/**
 *  <#Description#>
 */
@property (nonatomic, weak) UIImageView *backgroundImageView;

/**
 *  线
 */
@property (nonatomic, weak) UIImageView *scanLineImageView;

@end

@implementation ViewController

- (UIImageView *)scanLineImageView
{
    if (!_scanLineImageView)
    {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"qr_scan_line"]];
        imageView.frame = CGRectMake(0, 0, self.backgroundImageView.frame.size.width, 1);
        [self.backgroundImageView addSubview:imageView];
        
        _scanLineImageView = imageView;
    }
    
    return _scanLineImageView;
}

- (UIImageView *)backgroundImageView
{
    if (!_backgroundImageView)
    {
        int space = 40;
        
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"smk"]];
        imageView.frame = CGRectMake(space, space, self.view.frame.size.width - 2 * space, self.view.frame.size.width - 2 * space);
        [self.view addSubview:imageView];
        
        _backgroundImageView = imageView;
    }
    
    return _backgroundImageView;
}

- (AVCaptureSession *)session
{
    if (!_session)
    {
        _session = [[AVCaptureSession alloc] init];
    }
    
    return _session;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    /*
     AVCaptureDevice 设备
     AVCaptureDeviceInput 输入设备
     AVCaptureMetadataOutput 输出设备
     AVCaptureSession 输入和输出设备的桥梁,负责input和output数据交互。
     */
    
    

  
}

/**
 *  开始扫描
 *
 *  @param sender <#sender description#>
 */
- (IBAction)startScan:(id)sender
{
    if (!_session)
    {
        //1.设备
        AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
        
        //2.输入设备
        AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
        
        //3.输出设备
        AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
        //设置代理
        [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
        
        output.metadataObjectTypes = @[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code];

//4.输入和输出设备的桥梁,负责input和output数据交互 if ([self.session canAddInput:input]) { //添加输入设备 [self.session addInput:input]; } if ([self.session canAddOutput:output]) { //添加输出设备 [self.session addOutput:output]; } //可用类型 //[output availableMetadataObjectTypes]; //5. /* AVMetadataObjectTypeQRCode 二维码 */ //6.初始化要显示图像的layer //显示拍摄视频图像 AVCaptureVideoPreviewLayer *layer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session]; layer.frame = self.view.bounds; layer.contentsGravity = kCAGravityResizeAspectFill; [self.view.layer insertSublayer:layer atIndex:0]; } //7.开始扫描 [self.session startRunning]; [self animationScan];}#pragma mark - 代理方法(可能会调用多次)- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{ //扫描成功 if (metadataObjects.count > 0) { //如果扫描成功就停止扫描 [self.session stopRunning]; //结果类 AVMetadataMachineReadableCodeObject *object = metadataObjects[0]; //获取扫描的数据 NSString *value = [object stringValue]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:value delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil]; [alert show]; } }#pragma mark -/** * 扫描动画 */- (void)animationScan{ [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(updateScanLineFrame) userInfo:nil repeats:YES];}bool upMove;/** * 更新线的坐标 */- (void)updateScanLineFrame{ if (self.scanLineImageView.frame.origin.y <= 20) { //向下移动 upMove = NO; } else if(self.scanLineImageView.frame.origin.y >= self.backgroundImageView.frame.size.height - 20) { //向上移动 upMove = YES; } CGRect frame = self.scanLineImageView.frame; if (upMove) { frame.origin.y--; } else { frame.origin.y++; } //重新修改坐标 self.scanLineImageView.frame = frame; }- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
 
  

你可能感兴趣的:(iOS,UI)