正文
在二维码界面中写一个block来传递必要的参数,部分代码如下
typedef void(^QRUrlBlock)(NSString *url,NSString* invite_code,BOOL isResist);
@interface QRViewController : UIViewController<UIAlertViewDelegate>
@property (nonatomic, copy) QRUrlBlock qrUrlBlock;
@end
.m文件中调用相机,导入#import <AVFoundation/AVFoundation.h>
遵循AVCaptureMetadataOutputObjectsDelegate
部分代码如下:
@interface QRViewController ()<AVCaptureMetadataOutputObjectsDelegate,QRViewDelegate>
@property (strong, nonatomic) AVCaptureDevice * device;
@property (strong, nonatomic) AVCaptureDeviceInput * input;
@property (strong, nonatomic) AVCaptureMetadataOutput * output;
@property (strong, nonatomic) AVCaptureSession * session;
@property (strong, nonatomic) AVCaptureVideoPreviewLayer * preview;
@end
@implementation QRViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
// Input
_input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];
// Output
_output = [[AVCaptureMetadataOutput alloc]init];
[_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
// Session
_session = [[AVCaptureSession alloc]init];
[_session setSessionPreset:AVCaptureSessionPresetHigh];
if ([_session canAddInput:self.input])
{
[_session addInput:self.input];
}
if ([_session canAddOutput:self.output])
{
[_session addOutput:self.output];
}
if([[[UIDevice currentDevice] systemVersion] floatValue]>= 7.0)
{
//判断相机是否能够使用
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if