最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

掃描二維碼效果

 

源碼: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實體對象
 * @param codeString 掃描字符串
 */
- (void)QRCodeView:(QRCodeView *)codeView codeString:(NSString *)codeString;

@end

@interface QRCodeView : UIView

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

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

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

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

/**
 * 正在運行當中
 */
@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)建會話
 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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • iOS自定義UIBarButtonItem的target和action示例代碼

    iOS自定義UIBarButtonItem的target和action示例代碼

    這篇文章主要給大家介紹了關(guān)于iOS自定義UIBarButtonItem的target和action的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-02-02
  • 詳解IOS中如何實現(xiàn)瀑布流效果

    詳解IOS中如何實現(xiàn)瀑布流效果

    說到瀑布流, 或許大家都不陌生, 瀑布流的實現(xiàn)也有很多種! 從scrollView 到 tableView 書寫的瀑布流, 然后再到2012年iOS6 蘋果API新加進的collectionView進行的瀑布流封裝! 確實,不論是寫起來還是用起來都要方便很多!那么下面一起來看看IOS中具體如何實現(xiàn)瀑布流效果。
    2016-08-08
  • ios7中UIViewControllerBasedStatusBarAppearance作用詳解

    ios7中UIViewControllerBasedStatusBarAppearance作用詳解

    這篇文章主要介紹了 ios7中UIViewControllerBasedStatusBarAppearance作用詳解的相關(guān)資料,需要的朋友可以參考下
    2016-11-11
  • iOS中精確計算WebView高度的方法示例

    iOS中精確計算WebView高度的方法示例

    這篇文章主要給大家介紹了關(guān)于iOS中如何精確計算WebView高度,以及iOS開發(fā)之解決WebView自適應(yīng)內(nèi)容高度的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧
    2018-05-05
  • iOS NSURLProtocol的具體使用方法詳解

    iOS NSURLProtocol的具體使用方法詳解

    本篇文章主要介紹了iOS NSURLProtocol的具體使用方法詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • 完整的iOS新浪微博分享功能開發(fā)

    完整的iOS新浪微博分享功能開發(fā)

    這篇文章主要為大家詳細介紹了較為完整的iOS新浪微博分享功能開發(fā)的相關(guān)資料,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • 簡介Objective-C解析XML與JSON數(shù)據(jù)格式的方法

    簡介Objective-C解析XML與JSON數(shù)據(jù)格式的方法

    這篇文章主要介紹了Objective-C解析XML與JSON數(shù)據(jù)格式的方法,文中解析JSON包括拼接JSON字符串用到了SBJson這個解析器,需要的朋友可以參考下
    2016-01-01
  • IOS代碼筆記之文字走馬燈效果

    IOS代碼筆記之文字走馬燈效果

    這篇文章主要為大家詳細介紹了IOS實現(xiàn)文字走馬燈效果的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-07-07
  • iOS實現(xiàn)多個垂直滑動條并列視圖

    iOS實現(xiàn)多個垂直滑動條并列視圖

    這篇文章主要為大家詳細介紹了iOS實現(xiàn)多個垂直滑動條并列視圖,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Flutter?ScrollController滾動監(jiān)聽及控制示例詳解

    Flutter?ScrollController滾動監(jiān)聽及控制示例詳解

    這篇文章主要為大家介紹了Flutter?ScrollController滾動監(jiān)聽及控制示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11

最新評論

清涧县| 云梦县| 集安市| 南城县| 宜城市| 章丘市| 阿荣旗| 宜宾县| 邢台市| 正定县| 信阳市| 古蔺县| 阿尔山市| 陈巴尔虎旗| 吉林省| 晋宁县| 龙泉市| 滁州市| 仙桃市| 岫岩| 益阳市| 区。| 平谷区| 万安县| 伊吾县| 小金县| 奇台县| 龙山县| 项城市| 福建省| 西乌珠穆沁旗| 方城县| 宝山区| 福清市| 英超| 古蔺县| 定南县| 搜索| 海丰县| 屯昌县| 金湖县|