-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUploadImageViewController.m
More file actions
executable file
·144 lines (103 loc) · 4.32 KB
/
Copy pathUploadImageViewController.m
File metadata and controls
executable file
·144 lines (103 loc) · 4.32 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
137
138
139
140
141
142
143
144
//
// TPUploadImageViewController.m
// TutorialBase
//
// Created by Antonio MG on 7/4/12.
// Copyright (c) 2012 AMG. All rights reserved.
//
#import "UploadImageViewController.h"
#import <Parse/Parse.h>
@interface UploadImageViewController ()
@end
@implementation UploadImageViewController
@synthesize imgToUpload = _imgToUpload;
@synthesize username = _username;
@synthesize commentTextField = _commentTextField;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
self.imgToUpload = nil;
self.username = nil;
self.commentTextField = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark IB Actions
-(IBAction)selectPicturePressed:(id)sender
{
//Open a UIImagePickerController to select the picture
UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
imgPicker.delegate = self;
imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self.navigationController presentModalViewController:imgPicker animated:YES];
}
-(IBAction)sendPressed:(id)sender
{
[self.commentTextField resignFirstResponder];
//Disable the send button until we are ready
self.navigationItem.rightBarButtonItem.enabled = NO;
//Place the loading spinner
UIActivityIndicatorView *loadingSpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[loadingSpinner setCenter:CGPointMake(self.view.frame.size.width/2.0, self.view.frame.size.height/2.0)];
[loadingSpinner startAnimating];
[self.view addSubview:loadingSpinner];
//TODO: Upload a new picture
NSData *pictureData = UIImagePNGRepresentation(self.imgToUpload.image);
PFFile *file = [PFFile fileWithName:@"img" data:pictureData];
[file saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (succeeded) {
//Add the image to the object, and add the comment and the user
PFObject *imageObject = [PFObject objectWithClassName:@"WallImageObject"];
[imageObject setObject:file forKey:@"image"];
[imageObject setObject:[PFUser currentUser].username forKey:@"user"];
[imageObject setObject:self.commentTextField.text forKey:@"comment"];
[imageObject saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (succeeded) {
//Go back to the wall
[self.navigationController popViewControllerAnimated:YES];
}
else {
NSString *errorString = [[error userInfo] objectForKey:@"error"];
UIAlertView *errorAlertView = [[UIAlertView alloc] initWithTitle:@"Error" message:errorString delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[errorAlertView show];
}
}];
}
else {
NSString *errorString = [[error userInfo] objectForKey:@"error"];
UIAlertView *errorAlertView = [[UIAlertView alloc] initWithTitle:@"Error" message:errorString delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[errorAlertView show];
}
} progressBlock:^(int percentDone) {
NSLog(@"Uploaded: %d %%", percentDone);
}];
}
#pragma mark UIImagePicker delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo
{
[picker dismissModalViewControllerAnimated:YES];
//Place the image in the imageview
self.imgToUpload.image = img;
}
#pragma mark Error View
-(void)showErrorView:(NSString *)errorMsg
{
UIAlertView *errorAlertView = [[UIAlertView alloc] initWithTitle:@"Error" message:errorMsg delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[errorAlertView show];
}
@end