Skip to content

Commit ab9a382

Browse files
committed
CameraVC/Preview Image
1 parent 5a5a052 commit ab9a382

18 files changed

+2434
-1
lines changed

Base.lproj/Main.storyboard

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@
474474
<!--Camera-->
475475
<scene sceneID="8uC-gG-KWm">
476476
<objects>
477-
<viewController id="SM4-y3-xpt" sceneMemberID="viewController">
477+
<viewController id="SM4-y3-xpt" customClass="HomeViewController" sceneMemberID="viewController">
478478
<layoutGuides>
479479
<viewControllerLayoutGuide type="top" id="86Y-B8-jto"/>
480480
<viewControllerLayoutGuide type="bottom" id="KvM-UM-PNs"/>
@@ -486,11 +486,52 @@
486486
</view>
487487
<tabBarItem key="tabBarItem" tag="3" title="Camera" image="cam-1" id="zUJ-Qh-kXg"/>
488488
<simulatedScreenMetrics key="simulatedDestinationMetrics" type="retina47"/>
489+
<connections>
490+
<segue destination="OCu-8m-Wig" kind="presentation" identifier="Show Image Preview" id="2vf-At-lQr"/>
491+
</connections>
489492
</viewController>
490493
<placeholder placeholderIdentifier="IBFirstResponder" id="W7s-He-2cv" userLabel="First Responder" sceneMemberID="firstResponder"/>
491494
</objects>
492495
<point key="canvasLocation" x="549.5" y="1668.5"/>
493496
</scene>
497+
<!--Preview Image View Controller-->
498+
<scene sceneID="hmx-yw-Bbg">
499+
<objects>
500+
<viewController id="OCu-8m-Wig" customClass="PreviewImageViewController" customModule="Dindr" customModuleProvider="target" sceneMemberID="viewController">
501+
<layoutGuides>
502+
<viewControllerLayoutGuide type="top" id="VMF-bL-TCs"/>
503+
<viewControllerLayoutGuide type="bottom" id="rPa-cO-3Xd"/>
504+
</layoutGuides>
505+
<view key="view" contentMode="scaleToFill" id="3AV-vp-aDO">
506+
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
507+
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
508+
<subviews>
509+
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="2K4-aD-3Yb">
510+
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
511+
</imageView>
512+
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="A6h-Ty-NLA">
513+
<rect key="frame" x="313" y="28" width="46" height="30"/>
514+
<state key="normal" title="Done">
515+
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
516+
</state>
517+
</button>
518+
</subviews>
519+
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
520+
<constraints>
521+
<constraint firstItem="rPa-cO-3Xd" firstAttribute="top" secondItem="2K4-aD-3Yb" secondAttribute="bottom" id="A7D-qv-7Ia"/>
522+
<constraint firstItem="2K4-aD-3Yb" firstAttribute="leading" secondItem="3AV-vp-aDO" secondAttribute="leading" id="AxC-l6-yyt"/>
523+
<constraint firstItem="2K4-aD-3Yb" firstAttribute="top" secondItem="3AV-vp-aDO" secondAttribute="top" id="TJs-WD-q0x"/>
524+
<constraint firstAttribute="trailing" secondItem="2K4-aD-3Yb" secondAttribute="trailing" id="o5o-OW-u5d"/>
525+
</constraints>
526+
</view>
527+
<connections>
528+
<outlet property="imageView" destination="2K4-aD-3Yb" id="cWo-Qv-Vh4"/>
529+
</connections>
530+
</viewController>
531+
<placeholder placeholderIdentifier="IBFirstResponder" id="ZjH-0y-SCf" userLabel="First Responder" sceneMemberID="firstResponder"/>
532+
</objects>
533+
<point key="canvasLocation" x="491.5" y="2516.5"/>
534+
</scene>
494535
<!--Perks-->
495536
<scene sceneID="uaV-wT-OSC">
496537
<objects>

HomeViewController.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// HomeViewController.h
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 <UIKit/UIKit.h>
10+
#import "LLSimpleCamera.h"
11+
12+
@interface HomeViewController : UIViewController
13+
14+
@end

HomeViewController.m

Lines changed: 292 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,292 @@
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+

ImageViewController.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// ImageViewController.h
3+
// LLSimpleCameraExample
4+
//
5+
// Created by Ömer Faruk Gül on 15/11/14.
6+
// Copyright (c) 2014 Ömer Faruk Gül. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
@interface ImageViewController : UIViewController
12+
- (instancetype)initWithImage:(UIImage *)image;
13+
@end

0 commit comments

Comments
 (0)