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