-
Notifications
You must be signed in to change notification settings - Fork 1
/
WeddingSnapViewController.m
executable file
·236 lines (175 loc) · 6.66 KB
/
WeddingSnapViewController.m
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
//
// WeddingSnapViewController.m
// WeddingSnap
//
// Created by Neel Banerjee on 3/26/11.
// Copyright 2011 Om Design LLC. All rights reserved.
//
#import "WeddingSnapViewController.h"
#import "UIImage+fixOrientation.h"
#define COMPRESSION 0.8
@implementation WeddingSnapViewController
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
NSLog(@"In Here");
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
/*
// Custom initialization
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
cameraController = [[UIImagePickerController alloc] initWithNibName:nil bundle:nil];
cameraController.delegate = self;
cameraController.mediaTypes =
[UIImagePickerController availableMediaTypesForSourceType:
UIImagePickerControllerSourceTypeCamera];
NSLog(@"Camera Support");
}
else {
NSLog(@"No Camera Support");
}
*/
}
return self;
}
-(void)viewDidAppear:(BOOL)animated {
if([self startCameraControllerFromViewController:self
usingDelegate:self]) {
NSLog(@"True");
}
else {
NSLog(@"False");
}
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
/*
- (void)viewDidLoad {
//if(cameraController) {
//[self presentModalViewController:cameraController animated:YES];
//}
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
- (BOOL) startCameraControllerFromViewController: (UIViewController*) controller
usingDelegate: (id <UIImagePickerControllerDelegate,
UINavigationControllerDelegate>) delegate {
if (([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera] == NO)
|| (delegate == nil)
|| (controller == nil))
return NO;
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
// Displays a control that allows the user to choose picture or
// movie capture, if both are available:
cameraUI.mediaTypes =
[UIImagePickerController availableMediaTypesForSourceType:
UIImagePickerControllerSourceTypeCamera];
// Hides the controls for moving & scaling pictures, or for
// trimming movies. To instead show the controls, use YES.
cameraUI.allowsEditing = NO;
cameraUI.delegate = delegate;
[controller presentModalViewController: cameraUI animated: YES];
return YES;
}
// For responding to the user tapping Cancel.
- (void) imagePickerControllerDidCancel: (UIImagePickerController *) picker {
[[picker parentViewController] dismissModalViewControllerAnimated: YES];
[picker release];
}
// For responding to the user accepting a newly-captured picture or movie
- (void) imagePickerController: (UIImagePickerController *) picker
didFinishPickingMediaWithInfo: (NSDictionary *) info {
[[picker parentViewController] dismissModalViewControllerAnimated: YES];
[picker release];
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
UIImage *originalImage, *editedImage, *imageToSave;
// Handle a still image capture
if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0)
== kCFCompareEqualTo) {
editedImage = (UIImage *) [info objectForKey:
UIImagePickerControllerEditedImage];
originalImage = (UIImage *) [info objectForKey:
UIImagePickerControllerOriginalImage];
if (editedImage) {
imageToSave = editedImage;
//[imageToSave retain];
} else {
imageToSave = originalImage;
//[imageToSave retain];
}
[[self taskWithData:imageToSave] start];
}
// Handle a movie capture
if (CFStringCompare ((CFStringRef) mediaType, kUTTypeMovie, 0)
== kCFCompareEqualTo) {
NSString *moviePath = [[info objectForKey:
UIImagePickerControllerMediaURL] path];
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (moviePath)) {
UISaveVideoAtPathToSavedPhotosAlbum (
moviePath, nil, nil, nil);
}
[[picker parentViewController] dismissModalViewControllerAnimated: YES];
[picker release];
}
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *responseString = [request responseString];
NSLog(responseString);
// Use when fetching binary data
//NSData *responseData = [request responseData];
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
}
- (NSOperation*)taskWithData:(id)data {
NSInvocationOperation* theOp = [[[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(myTaskMethod:) object:data] autorelease];
return theOp;
}
// This is the method that does the actual work of the task.
- (void)myTaskMethod:(id)imageToSave {
// Perform the task.
// Save the new image (original or edited) to the Camera Roll
UIImageWriteToSavedPhotosAlbum (imageToSave, nil, nil , nil);
imageToSave = [imageToSave fixOrientation];
//Get Date for unique file name
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//[dateFormatter setDateStyle:NSDateFormatterShortStyle];
//[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setDateFormat:@"yyyyddMMHHmmss"];
NSString *dateTime = [dateFormatter stringFromDate:[NSDate date]];
[dateFormatter release];
//Send image to the server
NSData *jpg = UIImageJPEGRepresentation(imageToSave,COMPRESSION);
NSString *deviceName = [[[[UIDevice currentDevice] name]
stringByReplacingOccurrencesOfString:@"’" withString:@""]
stringByReplacingOccurrencesOfString:@" " withString:@""];
NSURL *url = [NSURL URLWithString:@"http://omdesignllc.com/postPic.php"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setData:jpg withFileName:[NSString stringWithFormat:@"%@%@.jpg",deviceName,dateTime] andContentType:@"image/jpeg" forKey:@"photo"];
[request setDelegate:self];
[request startAsynchronous];
}
@end