@@ -65,8 +65,6 @@ - (AVCaptureSession *)captureSession
65
65
{
66
66
if (!_captureSession)
67
67
{
68
- _captureSession = [[AVCaptureSession alloc ] init ];
69
-
70
68
NSError *error = nil ;
71
69
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType: AVMediaTypeVideo];
72
70
if (device.isAutoFocusRangeRestrictionSupported )
@@ -78,36 +76,42 @@ - (AVCaptureSession *)captureSession
78
76
}
79
77
}
80
78
79
+ // The first time AVCaptureDeviceInput creation will present a dialog to the user
80
+ // requesting camera access. If the user refuses the creation fails.
81
+ // See WWDC 2013 session #610 for details, but note this behaviour does not seem to
82
+ // be enforced on iOS 7 where as it is with iOS 8.
83
+
81
84
AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice: device error: &error];
82
85
if (deviceInput)
83
86
{
87
+ _captureSession = [[AVCaptureSession alloc ] init ];
84
88
if ([_captureSession canAddInput: deviceInput])
85
89
{
86
90
[_captureSession addInput: deviceInput];
87
91
}
92
+
93
+ AVCaptureMetadataOutput *metadataOutput = [[AVCaptureMetadataOutput alloc ] init ];
94
+ if ([_captureSession canAddOutput: metadataOutput])
95
+ {
96
+ [_captureSession addOutput: metadataOutput];
97
+ [metadataOutput setMetadataObjectsDelegate: self queue: dispatch_get_main_queue ()];
98
+ [metadataOutput setMetadataObjectTypes: @[AVMetadataObjectTypeQRCode]];
99
+ }
100
+
101
+ self.previewLayer = [[AVCaptureVideoPreviewLayer alloc ] initWithSession: _captureSession];
102
+ self.previewLayer .videoGravity = AVLayerVideoGravityResizeAspectFill;
103
+ self.previewLayer .frame = self.view .bounds ;
104
+ [self .view.layer addSublayer: self .previewLayer];
105
+
106
+ self.targetLayer = [CALayer layer ];
107
+ self.targetLayer .frame = self.view .bounds ;
108
+ [self .view.layer addSublayer: self .targetLayer];
109
+
88
110
}
89
111
else
90
112
{
91
113
NSLog (@" Input Device error: %@ " ,[error localizedDescription ]);
92
114
}
93
-
94
- AVCaptureMetadataOutput *metadataOutput = [[AVCaptureMetadataOutput alloc ] init ];
95
- if ([_captureSession canAddOutput: metadataOutput])
96
- {
97
- [_captureSession addOutput: metadataOutput];
98
- [metadataOutput setMetadataObjectsDelegate: self queue: dispatch_get_main_queue ()];
99
- [metadataOutput setMetadataObjectTypes: @[AVMetadataObjectTypeQRCode]];
100
- }
101
-
102
- self.previewLayer = [[AVCaptureVideoPreviewLayer alloc ] initWithSession: _captureSession];
103
- self.previewLayer .videoGravity = AVLayerVideoGravityResizeAspectFill;
104
- self.previewLayer .frame = self.view .bounds ;
105
- [self .view.layer addSublayer: self .previewLayer];
106
-
107
- self.targetLayer = [CALayer layer ];
108
- self.targetLayer .frame = self.view .bounds ;
109
- [self .view.layer addSublayer: self .targetLayer];
110
-
111
115
}
112
116
return _captureSession;
113
117
}
0 commit comments