欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

掃描二維碼控件的封裝iOS實(shí)現(xiàn)

 更新時(shí)間:2016年08月10日 10:03:57   作者:YouXianMing  
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)掃描二維碼控件的封裝,具有一定的實(shí)用性和參考價(jià)值,感興趣的小伙伴們可以參考一下

掃描二維碼效果

 

源碼:https://github.com/YouXianMing/Animations 

//
// QRCodeView.h
// QRCode
//
// Created by YouXianMing on 16/7/7.
// Copyright © 2016年 XianMing You. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@class QRCodeView;

@protocol QRCodeViewDelegate <NSObject>

@optional

/**
 * 獲取QR的掃描結(jié)果
 *
 * @param codeView QRCodeView實(shí)體對象
 * @param codeString 掃描字符串
 */
- (void)QRCodeView:(QRCodeView *)codeView codeString:(NSString *)codeString;

@end

@interface QRCodeView : UIView

/**
 * 代理
 */
@property (nonatomic, weak) id <QRCodeViewDelegate> delegate;

/**
 * 燈的狀態(tài),默認(rèn)為關(guān)閉
 */
@property (nonatomic) AVCaptureTorchMode torchMode;

/**
 * 敏感區(qū)域,如果不設(shè)置,則為全部掃描區(qū)域
 */
@property (nonatomic) CGRect interestArea;

/**
 * 你用來添加自定義控件的view,尺寸與當(dāng)前初始化的view一致
 */
@property (nonatomic, strong) UIView *contentView;

/**
 * 正在運(yùn)行當(dāng)中
 */
@property (nonatomic, readonly) BOOL isRunning;

/**
 * 開始掃描
 *
 * @return 如果成功,則返回YES,否則返回NO
 */
- (BOOL)start;

/**
 * 結(jié)束掃描
 */
- (void)stop;

@end

//
// QRCodeView.m
// QRCode
//
// Created by YouXianMing on 16/7/7.
// Copyright © 2016年 XianMing You. All rights reserved.
//

#import "QRCodeView.h"

@interface QRCodeView () <AVCaptureMetadataOutputObjectsDelegate>

@property (nonatomic) BOOL         isRunning;
@property (nonatomic, strong) UIView      *videoView;

@property (nonatomic, strong) AVCaptureDeviceInput  *deviceInput;
@property (nonatomic, strong) AVCaptureDevice    *captureDevice;
@property (nonatomic, strong) AVCaptureSession   *captureSession;
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *videoPreviewLayer;
@property (nonatomic, strong) AVCaptureMetadataOutput  *captureMetadataOutput;

@end

@implementation QRCodeView

- (instancetype)initWithFrame:(CGRect)frame {
 
 if (self = [super initWithFrame:frame]) {
  
  self.videoView = [[UIView alloc] initWithFrame:self.bounds];
  [self addSubview:self.videoView];
  
  self.contentView = [[UIView alloc] initWithFrame:self.bounds];
  [self addSubview:self.contentView];
  
  self.captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
  
  _torchMode = AVCaptureTorchModeOff;
  
  [self addNotificationCenter];
 }
 
 return self;
}

#pragma mark - NSNotificationCenter related.

- (void)addNotificationCenter {
 
 [[NSNotificationCenter defaultCenter] addObserver:self
            selector:@selector(notificationCenterEvent:)
             name:AVCaptureInputPortFormatDescriptionDidChangeNotification
            object:nil];
}

- (void)removeNotificationCenter {
 
 [[NSNotificationCenter defaultCenter] removeObserver:self
             name:AVCaptureInputPortFormatDescriptionDidChangeNotification
             object:nil];
}

- (void)notificationCenterEvent:(NSNotification *)sender {
 
 if (self.interestArea.size.width && self.interestArea.size.height) {
  
  self.captureMetadataOutput.rectOfInterest = [self.videoPreviewLayer metadataOutputRectOfInterestForRect:self.interestArea];
  
 } else {
  
  self.captureMetadataOutput.rectOfInterest = CGRectMake(0, 0, 1, 1);
 }
}

#pragma mark - Start & Stop.

- (BOOL)start {
 
 // 初始化輸入流
 BOOL  result = NO;
 NSError *error = nil;
 self.deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:self.captureDevice error:&error];
 if (self.deviceInput == nil) {
  
  NSLog(@"%@", error);
  return result;
 }
 
 // 創(chuàng)建會(huì)話
 self.captureSession = [[AVCaptureSession alloc] init];
 
 // 添加輸入流
 [self.captureSession addInput:self.deviceInput];
 
 // 初始化輸出流
 self.captureMetadataOutput = [[AVCaptureMetadataOutput alloc] init];
 
 // 添加輸出流
 [self.captureSession addOutput:self.captureMetadataOutput];
 
 // 創(chuàng)建queue.
 [self.captureMetadataOutput setMetadataObjectsDelegate:self queue:dispatch_queue_create(nil, nil)];
 self.captureMetadataOutput.metadataObjectTypes = @[AVMetadataObjectTypeQRCode];
 
 // 創(chuàng)建輸出對象
 self.videoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];
 self.videoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
 self.videoPreviewLayer.frame = self.contentView.bounds;
 [self.videoView.layer addSublayer:self.videoPreviewLayer];
 
 // 開始
 [self.captureSession startRunning];
 self.isRunning = YES;
 result   = YES;
 
 return result;
}

- (void)stop {
 
 [self.captureSession stopRunning];
 self.isRunning  = NO;
 self.captureSession = nil;
}

#pragma mark - AVCaptureMetadataOutputObjectsDelegate

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects
  fromConnection:(AVCaptureConnection *)connection {
 
 if (metadataObjects.count > 0) {
  
  AVMetadataMachineReadableCodeObject *metadata = metadataObjects.firstObject;
  NSString       *result = nil;
  
  if ([metadata.type isEqualToString:AVMetadataObjectTypeQRCode]) {
   
   result = metadata.stringValue;
   
   if (_delegate && [_delegate respondsToSelector:@selector(QRCodeView:codeString:)]) {
    
    [_delegate QRCodeView:self codeString:result];
   }
  }
 }
}

#pragma mark - Setter & Getter.

- (void)setTorchMode:(AVCaptureTorchMode)torchMode {

 _torchMode = torchMode;
 
 if (_deviceInput && [self.captureDevice hasTorch]) {
  
  [self.captureDevice lockForConfiguration:nil];
  [self.captureDevice setTorchMode:torchMode];
  [self.captureDevice unlockForConfiguration];
 }
}

#pragma mark - System method.

- (void)dealloc {
 
 [self stop];
 [self removeNotificationCenter];
}

@end

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • IOS自適配利器Masonry使用指南

    IOS自適配利器Masonry使用指南

    如果說自動(dòng)布局解救了多屏幕適配,那眾多三方庫的出現(xiàn)就解救了系統(tǒng)自動(dòng)布局的寫法。Masonry就是其中一個(gè)。用法上也比較簡單靈活,很大程度上替代了傳統(tǒng)的NSLayoutConstraint布局方式。下面我們就來具體探討下吧
    2016-01-01
  • 詳解iOS開發(fā)中使用storyboard創(chuàng)建導(dǎo)航控制器的方法

    詳解iOS開發(fā)中使用storyboard創(chuàng)建導(dǎo)航控制器的方法

    這篇文章主要介紹了iOS開發(fā)中使用storyboard創(chuàng)建導(dǎo)航控制器的方法,包括對控制器聲明周期的控制介紹,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下
    2016-01-01
  • iOS高仿微信表情輸入功能代碼分享

    iOS高仿微信表情輸入功能代碼分享

    最近項(xiàng)目需求,要實(shí)現(xiàn)一個(gè)類似微信的的表情輸入功能,今天小編抽空給大家分享iOS高仿微信表情輸入功能代碼,非常不錯(cuò),感興趣的朋友參考下吧
    2016-11-11
  • iOS仿微信搖一搖功能

    iOS仿微信搖一搖功能

    這篇文章主要為大家詳細(xì)介紹了iOS仿微信搖一搖功能的實(shí)現(xiàn)方法 ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • iOS導(dǎo)航欄控制的一些總結(jié)

    iOS導(dǎo)航欄控制的一些總結(jié)

    這篇文章主要給大家介紹了關(guān)于iOS導(dǎo)航欄控制的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對各位iOS開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • 揭開iOS逆向解密的神秘面紗

    揭開iOS逆向解密的神秘面紗

    這篇文章主要給大家介紹了關(guān)于iOS逆向解密的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-06-06
  • iOS中tableview實(shí)現(xiàn)編輯、全選及刪除等功能的方法示例

    iOS中tableview實(shí)現(xiàn)編輯、全選及刪除等功能的方法示例

    這篇文章主要給大家介紹了關(guān)于iOS中tableview實(shí)現(xiàn)編輯、全選及刪除等功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),不僅是介紹實(shí)現(xiàn)的方法,將實(shí)現(xiàn)過程中遇到的問題也都分享出來了,需要的朋友們下面來一起看看吧。
    2017-07-07
  • Objective-C的UIStackView常用屬性函數(shù)學(xué)習(xí)筆記

    Objective-C的UIStackView常用屬性函數(shù)學(xué)習(xí)筆記

    這篇文章主要為大家介紹了Objective-C的UIStackView常用屬性函數(shù)學(xué)習(xí)筆記,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • IOS 簽名錯(cuò)誤codesign failed with exit code 1解決方法

    IOS 簽名錯(cuò)誤codesign failed with exit code 1解決方法

    這篇文章主要介紹了IOS 簽名錯(cuò)誤codesign failed with exit code 1解決方法的相關(guān)資料,遇到同樣問題的朋友可以看下,這里提供了解決方案,需要的朋友可以參考下
    2017-01-01
  • iOS中使用NSProgress類來創(chuàng)建UI進(jìn)度條的方法詳解

    iOS中使用NSProgress類來創(chuàng)建UI進(jìn)度條的方法詳解

    NSProgress是iOS7以后引入的用于制作進(jìn)度條的類,能夠監(jiān)聽多個(gè)任務(wù),這里就為大家?guī)韎OS中使用NSProgress類來創(chuàng)建UI進(jìn)度條的方法詳解,需要的朋友可以參考下
    2016-06-06

最新評論