// // 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