-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHomeViewController.m
More file actions
292 lines (221 loc) · 10.5 KB
/
HomeViewController.m
File metadata and controls
292 lines (221 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
//
// HomeViewController.m
// LLSimpleCameraExample
//
// Created by Ömer Faruk Gül on 29/10/14.
// Copyright (c) 2014 Ömer Faruk Gül. All rights reserved.
//
#import "HomeViewController.h"
#import "ViewUtils.h"
#import "ImageViewController.h"
#import "VideoViewController.h"
#import "Dindr-Swift.h"
@interface HomeViewController ()
@property (strong, nonatomic) LLSimpleCamera *camera;
@property (strong, nonatomic) UILabel *errorLabel;
@property (strong, nonatomic) UIButton *snapButton;
@property (strong, nonatomic) UIButton *switchButton;
@property (strong, nonatomic) UIButton *flashButton;
@property (strong, nonatomic) UISegmentedControl *segmentedControl;
@end
@implementation HomeViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
[self.navigationController setNavigationBarHidden:YES animated:NO];
CGRect screenRect = [[UIScreen mainScreen] bounds];
// ----- initialize camera -------- //
// create camera vc
self.camera = [[LLSimpleCamera alloc] initWithQuality:AVCaptureSessionPresetHigh
position:CameraPositionBack
videoEnabled:YES];
// attach to a view controller
[self.camera attachToViewController:self withFrame:CGRectMake(0, 0, screenRect.size.width, screenRect.size.height)];
// read: http://stackoverflow.com/questions/5427656/ios-uiimagepickercontroller-result-image-orientation-after-upload
// you probably will want to set this to YES, if you are going view the image outside iOS.
self.camera.fixOrientationAfterCapture = NO;
// take the required actions on a device change
__weak typeof(self) weakSelf = self;
[self.camera setOnDeviceChange:^(LLSimpleCamera *camera, AVCaptureDevice * device) {
NSLog(@"Device changed.");
// device changed, check if flash is available
if([camera isFlashAvailable]) {
weakSelf.flashButton.hidden = NO;
if(camera.flash == CameraFlashOff) {
weakSelf.flashButton.selected = NO;
}
else {
weakSelf.flashButton.selected = YES;
}
}
else {
weakSelf.flashButton.hidden = YES;
}
}];
[self.camera setOnError:^(LLSimpleCamera *camera, NSError *error) {
NSLog(@"Camera error: %@", error);
if([error.domain isEqualToString:LLSimpleCameraErrorDomain]) {
if(error.code == LLSimpleCameraErrorCodeCameraPermission ||
error.code == LLSimpleCameraErrorCodeMicrophonePermission) {
if(weakSelf.errorLabel) {
[weakSelf.errorLabel removeFromSuperview];
}
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.text = @"We need permission for the camera.\nPlease go to your settings.";
label.numberOfLines = 2;
label.lineBreakMode = NSLineBreakByWordWrapping;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont fontWithName:@"AvenirNext-DemiBold" size:13.0f];
label.textColor = [UIColor whiteColor];
label.textAlignment = NSTextAlignmentCenter;
[label sizeToFit];
label.center = CGPointMake(screenRect.size.width / 2.0f, screenRect.size.height / 2.0f);
weakSelf.errorLabel = label;
[weakSelf.view addSubview:weakSelf.errorLabel];
}
}
}];
// ----- camera buttons -------- //
// snap button to capture image
self.snapButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.snapButton.frame = CGRectMake(0, 0, 70.0f, 70.0f);
self.snapButton.clipsToBounds = YES;
self.snapButton.layer.cornerRadius = self.snapButton.width / 2.0f;
self.snapButton.layer.borderColor = [UIColor whiteColor].CGColor;
self.snapButton.layer.borderWidth = 2.0f;
self.snapButton.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.5];
self.snapButton.layer.rasterizationScale = [UIScreen mainScreen].scale;
self.snapButton.layer.shouldRasterize = YES;
[self.snapButton addTarget:self action:@selector(snapButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.snapButton];
// button to toggle flash
self.flashButton = [UIButton buttonWithType:UIButtonTypeSystem];
self.flashButton.frame = CGRectMake(0, 0, 16.0f + 20.0f, 24.0f + 20.0f);
self.flashButton.tintColor = [UIColor whiteColor];
[self.flashButton setImage:[UIImage imageNamed:@"camera-flash.png"] forState:UIControlStateNormal];
self.flashButton.imageEdgeInsets = UIEdgeInsetsMake(10.0f, 10.0f, 10.0f, 10.0f);
[self.flashButton addTarget:self action:@selector(flashButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.flashButton];
// button to toggle camera positions
self.switchButton = [UIButton buttonWithType:UIButtonTypeSystem];
self.switchButton.frame = CGRectMake(0, 0, 29.0f + 20.0f, 22.0f + 20.0f);
self.switchButton.tintColor = [UIColor whiteColor];
[self.switchButton setImage:[UIImage imageNamed:@"camera-switch.png"] forState:UIControlStateNormal];
self.switchButton.imageEdgeInsets = UIEdgeInsetsMake(10.0f, 10.0f, 10.0f, 10.0f);
[self.switchButton addTarget:self action:@selector(switchButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.switchButton];
self.segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"Picture",@"Video"]];
self.segmentedControl.frame = CGRectMake(12.0f, screenRect.size.height - 67.0f, 120.0f, 32.0f);
self.segmentedControl.selectedSegmentIndex = 0;
self.segmentedControl.tintColor = [UIColor whiteColor];
[self.segmentedControl addTarget:self action:@selector(segmentedControlValueChanged:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:self.segmentedControl];
}
- (void)segmentedControlValueChanged:(UISegmentedControl *)control {
NSLog(@"Segment value changed!");
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// start the camera
[self.camera start];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
// stop the camera
[self.camera stop];
}
/* camera button methods */
- (void)switchButtonPressed:(UIButton *)button {
[self.camera togglePosition];
}
- (NSURL *)applicationDocumentsDirectory {
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory
inDomains:NSUserDomainMask] lastObject];
}
- (void)flashButtonPressed:(UIButton *)button {
if(self.camera.flash == CameraFlashOff) {
BOOL done = [self.camera updateFlashMode:CameraFlashOn];
if(done) {
self.flashButton.selected = YES;
self.flashButton.tintColor = [UIColor yellowColor];
}
}
else {
BOOL done = [self.camera updateFlashMode:CameraFlashOff];
if(done) {
self.flashButton.selected = NO;
self.flashButton.tintColor = [UIColor whiteColor];
}
}
}
- (void)snapButtonPressed:(UIButton *)button {
if(self.segmentedControl.selectedSegmentIndex == 0) {
// capture
[self.camera capture:^(LLSimpleCamera *camera, UIImage *image, NSDictionary *metadata, NSError *error) {
if(!error) {
// we should stop the camera, since we don't need it anymore. We will open a new vc.
// this very important, otherwise you may experience memory crashes
[camera stop];
// show the image
[self performSegueWithIdentifier:@"Show Image Preview" sender:image];
}
else {
NSLog(@"An error has occured: %@", error);
}
} exactSeenImage:YES];
}
else {
if(!self.camera.isRecording) {
self.segmentedControl.hidden = YES;
self.flashButton.hidden = YES;
self.switchButton.hidden = YES;
self.snapButton.layer.borderColor = [UIColor redColor].CGColor;
self.snapButton.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.5];
// start recording
NSURL *outputURL = [[[self applicationDocumentsDirectory]
URLByAppendingPathComponent:@"test1"] URLByAppendingPathExtension:@"mov"];
[self.camera startRecordingWithOutputUrl:outputURL];
}
else {
self.segmentedControl.hidden = NO;
self.flashButton.hidden = NO;
self.switchButton.hidden = NO;
self.snapButton.layer.borderColor = [UIColor whiteColor].CGColor;
self.snapButton.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.5];
[self.camera stopRecording:^(LLSimpleCamera *camera, NSURL *outputFileUrl, NSError *error) {
VideoViewController *vc = [[VideoViewController alloc] initWithVideoUrl:outputFileUrl];
[self.navigationController pushViewController:vc animated:YES];
}];
}
}
}
/* other lifecycle methods */
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
self.camera.view.frame = self.view.contentBounds;
self.snapButton.center = self.view.contentCenter;
self.snapButton.bottom = self.view.height - 15;
self.flashButton.center = self.view.contentCenter;
self.flashButton.top = 5.0f;
self.switchButton.top = 5.0f;
self.switchButton.right = self.view.width - 5.0f;
}
- (BOOL)prefersStatusBarHidden {
return YES;
}
- (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - Navigation
//#warning - Change your according image view controller here after taking the photo
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"Show Image Preview"]) {
PreviewImageViewController *pivc = segue.destinationViewController;
pivc.image = (UIImage *)sender;
}
}
@end