ios原生二維碼掃描與生成的實現(xiàn)教程
之前都是第三方的二維碼,操作簡單pod集成,美滋滋。隨著公司項目越來越惡心到大,還是自己擼一個比較實在。
demo的主要功能掃描二維碼和生成二維碼。
掃描相關(guān)類
二維碼掃描需要獲取攝像頭并讀取照片信息,因此我們需要導入系統(tǒng)的AVFoundation框架,創(chuàng)建視頻會話。我們需要用到一下幾個類:
- AVCaptureSession 會話對象。此類作為硬件設備輸入輸出信息的橋梁,承擔實時獲取設備數(shù)據(jù)的責任
- AVCaptureDeviceInput 設備輸入類。這個類用來表示輸入數(shù)據(jù)的硬件設備,配置抽象設備的port
- AVCaptureMetadataOutput 輸出類。這個支持二維碼、條形碼等圖像數(shù)據(jù)的識別
- AVCaptureVideoPreviewLayer 圖層類。用來快速呈現(xiàn)攝像頭獲取的原始數(shù)據(jù)
二維碼掃描功能的實現(xiàn)步驟是創(chuàng)建好會話對象,用來獲取從硬件設備輸入的數(shù)據(jù),并實時顯示在界面上。在掃描到相應圖像數(shù)據(jù)的時候,通過AVCaptureVideoPreviewLayer類型進行返回
掃描二維碼
1.首先倒入框架
#import <AVFoundation/AVFoundation.h>
2.遵守協(xié)議
<AVCaptureMetadataOutputObjectsDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
3.主要用到的屬性設置
//捕獲設備,默認后置攝像頭 @property (strong, nonatomic) AVCaptureDevice * device; //輸入設備 @property (strong, nonatomic) AVCaptureDeviceInput * input; //輸出設備,需要指定他的輸出類型及掃描范圍 @property (strong, nonatomic) AVCaptureMetadataOutput * output; //AVFoundation框架捕獲類的中心樞紐,協(xié)調(diào)輸入輸出設備以獲得數(shù)據(jù) @property (strong, nonatomic) AVCaptureSession * session; //展示捕獲圖像的圖層,是CALayer的子類 @property (strong, nonatomic) AVCaptureVideoPreviewLayer * preview;

4.拉起本地相冊二維碼
- (void)chooseButtonClick
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
//關(guān)閉掃描
[self stopScan];
//1 彈出系統(tǒng)相冊
UIImagePickerController *pickVC = [[UIImagePickerController alloc]init];
//2 設置照片來源
/**
UIImagePickerControllerSourceTypePhotoLibrary,相冊
UIImagePickerControllerSourceTypeCamera,相機
UIImagePickerControllerSourceTypeSavedPhotosAlbum,照片庫
*/
pickVC.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
//3 設置代理
pickVC.delegate = self;
//4.轉(zhuǎn)場動畫
self.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;
[self presentViewController:pickVC animated:YES completion:nil];
}
else
{
[self showAlertViewWithTitle:@"打開失敗" withMessage:@"相冊打開失敗。設備不支持訪問相冊,請在設置->隱私->照片中進行設置!"];
}
}
生成二維碼
1.二維碼的生成
/** * 2.生成CIFilter(濾鏡)對象 */ CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"]; /** * 3.恢復濾鏡默認設置 */ [filter setDefaults]; /** * 4.設置數(shù)據(jù)(通過濾鏡對象的KVC) */ //存放的信息 NSString *info = @"hahahahhahahaha"; //把信息轉(zhuǎn)化為NSData NSData *infoData = [info dataUsingEncoding:NSUTF8StringEncoding]; //濾鏡對象kvc存值 [filter setValue:infoData forKeyPath:@"inputMessage"]; /** * 5.生成二維碼 */ CIImage *outImage = [filter outputImage]; //imageView.image = [UIImage imageWithCIImage:outImage];//不處理圖片模糊,故而調(diào)用下面的信息 self.codeImage.image = [outImage createNonInterpolatedWithSize:150];
2.保存到本地相冊
UIGraphicsBeginImageContext(self.view.bounds.size); [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; //獲取圖片 UIImage * image = UIGraphicsGetImageFromCurrentImageContext(); //關(guān)閉上下文 UIGraphicsEndImageContext(); completion(image); /** * 將圖片保存到本地相冊 */ UIImageWriteToSavedPhotosAlbum(image, self , @selector(saveImage:didFinishSavingWithError:contextInfo:), nil);//保存圖片到照片庫
主要代碼貼出來,感興趣可以去gibHub地址:https://github.com/MrBMask 這里瞅瞅呦
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
利用iOS開發(fā)實現(xiàn)翻轉(zhuǎn)撲克牌動畫的方法
這篇文章主要給大家介紹了關(guān)于利用iOS開發(fā)實現(xiàn)翻撲克牌動畫的方法,文中通過示例代碼介紹的非常詳細,對大家具有一定的參考學習價值,需要的朋友們下面來跟著小編一起學習學習吧。2017-07-07
IOS 波紋進度(waveProgress)動畫實現(xiàn)
這篇文章主要介紹了IOS 紋進度(waveProgress)動畫實現(xiàn)的相關(guān)資料,需要的朋友可以參考下2016-09-09

