-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCameraViewController.m
More file actions
executable file
·136 lines (110 loc) · 4.49 KB
/
Copy pathCameraViewController.m
File metadata and controls
executable file
·136 lines (110 loc) · 4.49 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
//
// CameraViewController.m
// Munoz Studio App
//
// Created by daniel alejandro soraluz on 12/3/13.
// Copyright (c) 2013 daniel alejandro soraluz. All rights reserved.
//
#import "CameraViewController.h"
#import "Photos.h"
@interface CameraViewController ()
@end
@implementation CameraViewController
@synthesize takenPicture, imagePicker, appdel;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Set the title and image to be displayed on the tab bar.
self.title = @"Camera";
self.tabBarItem.image = [UIImage imageNamed:@"camera"];
appdel = [[UIApplication sharedApplication] delegate];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.tabBarController.delegate = self;
[self startCameraControllerFromViewController:self usingDelegate:self];
}
//JB
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
//loads the camera when the camera tab button is selected
if ([viewController.tabBarItem.title isEqualToString:@"Camera"])
{
[self startCameraControllerFromViewController: self
usingDelegate: self];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//DS
//Method that checks if the camera hardware is avaiable and presents it as the view of the window.
- (BOOL) startCameraControllerFromViewController: (UIViewController*) controller
usingDelegate: (id <UIImagePickerControllerDelegate,
UINavigationControllerDelegate>) delegate {
//Checks to see if the hardware supports a camera.
if (([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera] == NO)
|| (delegate == nil)
|| (controller == nil))
return NO;
//Create the picker object
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
//Specify the types of camera features available.
cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
//Shows the controls for moving & scaling pictures.
// To hide the controls, use NO
cameraUI.allowsEditing = YES;
//Set the delegate for the camera
cameraUI.delegate = delegate;
//Push the view onto the stack
[controller presentModalViewController: cameraUI animated: YES];
return YES;
}
//DS
//Taking the picture taken by the user and saving it to the core database
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//Create an image and store the acquired picture
UIImage *imageToSave;
imageToSave = (UIImage *) [info objectForKey:UIImagePickerControllerOriginalImage];
//Save the new image to the Camera Roll
UIImageWriteToSavedPhotosAlbum(imageToSave, nil, nil, nil);
[self AddPhoto:imageToSave];
//Tell controller to remove the picker from the view hierarchy and release object.
[self dismissViewControllerAnimated:YES completion:nil];
[self.tabBarController setSelectedIndex:0];
}
// For responding to the user tapping Cancel.
- (void) imagePickerControllerDidCancel: (UIImagePickerController *) picker
{
//dismisses the camera and loads the allphotosviewcontroller
[self dismissViewControllerAnimated: YES completion: nil];
[self.tabBarController setSelectedIndex:0];
}
//Method that adds a taken photo to core database
- (void) AddPhoto:(UIImage *) photo
{
NSManagedObjectContext *context = [appdel managedObjectContext];
Photos *photodata = [NSEntityDescription insertNewObjectForEntityForName:@"Photos" inManagedObjectContext:context];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:@"MMddmmss"];
NSString *stringDate = [dateFormat stringFromDate:[NSDate date]];
NSString *name = [stringDate stringByAppendingString:@".png"];
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(photo)];
photodata.name = name;
photodata.owner = @"default";
photodata.photo = imageData;
NSError *error;
if (![context save:&error]) {
NSLog(@"Couldn't create the subscription");
}
}
@end