I'm new to iOS development and currently creating a app for my major project in uni. I'm creating a registration page where the user is asked to fill in their details and also upload an image. So far I've got the values going to the database but I can't figure out how to also add the image that has been selected by the image picker. Could anyone help with this? The code is posted below....
RegistrationViewController.h
#import <UIKit/UIKit.h>
#define kpostURL @"http://myurl.com/myphp.php"
#define kfname @"fname"
#define ksname @"sname"
#define kemail @"email"
#define kusername @"username"
#define kpassword @"password"
#define kinstrument @"instrument"
#define klocation @"location"
@interface registrationViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate> {
UIScrollView *scrollView_;
BOOL keyboardVisible_;
IBOutlet UITextField *fnameText;
IBOutlet UITextField *snameText;
IBOutlet UITextField *emailText;
IBOutlet UITextField *usernameText;
IBOutlet UITextField *passwordText;
IBOutlet UITextField *instrumentText;
IBOutlet UITextField *locationText;
IBOutlet UIImageView *ImageView;
IBOutlet UIButton *button;
NSURLConnection *postConnection;
}
@property (nonatomic, retain) UIImagePickerController *imagePicker;
@property (nonatomic, strong) IBOutlet UIScrollView *scrollView;
- (IBAction) pickImage:(id)sender;
- (IBAction) textFieldDoneEditing: (id) sender;
-(IBAction)post:(id)sender;
-(void) postMessage:(NSString*) location withName:(NSString *) fname withSurname:(NSString *) sname withEmail:(NSString *) email withUsername:(NSString *) username withPassword:(NSString *) password withInstrument:(NSString *) instrument;
- (void) keyboardDidShow:(NSNotification *)notif;
- (void) keyboardDidHide:(NSNotification *)notif;
@end
RegistrationViewController.m
#import "registrationViewController.h"
#import <QuartzCore/QuartzCore.h>
@implementation registrationViewController
@synthesize imagePicker;
@synthesize scrollView=scrollView_;
- (IBAction)textFieldDoneEditing:(id)sender {
[sender resignFirstResponder];
}
- (IBAction)pickImage:(id)sender {
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:imagePicker animated:YES];
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{
ImageView.image = image;
[imagePicker dismissModalViewControllerAnimated:YES];
}
- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[imagePicker dismissModalViewControllerAnimated:YES];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
}
-(void) postMessage:(NSString*) location withName:(NSString *) fname withSurname:(NSString *) sname withEmail:(NSString *) email withUsername:(NSString *) username withPassword:(NSString *) password withInstrument:(NSString *) instrument{
if (fname != nil && location != nil && sname !=nil && email !=nil && username !=nil && password !=nil && instrument !=nil){
NSMutableString *postString = [NSMutableString stringWithString:kpostURL];
[postString appendString:[NSString stringWithFormat:@"?%@=%@", kfname, fname]];
[postString appendString:[NSString stringWithFormat:@"&%@=%@", ksname, sname]];
[postString appendString:[NSString stringWithFormat:@"&%@=%@", kemail, email]];
[postString appendString:[NSString stringWithFormat:@"&%@=%@", kusername, username]];
[postString appendString:[NSString stringWithFormat:@"&%@=%@", kpassword, password]];
[postString appendString:[NSString stringWithFormat:@"&%@=%@", kinstrument, instrument]];
[postString appendString:[NSString stringWithFormat:@"&%@=%@", klocation, location]];
[postString setString:[postString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:postString]];
[request setHTTPMethod:@"POST"];
postConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
}
}
-(IBAction)post:(id)sender{
[self postMessage:locationText.text withName:fnameText.text withSurname:snameText.text withEmail:emailText.text withUsername:usernameText.text withPassword:passwordText.text withInstrument:instrumentText.text];
[locationText resignFirstResponder];
fnameText.text = nil;
snameText.text = nil;
emailText.text = nil;
usernameText.text = nil;
passwordText.text = nil;
instrumentText.text = nil;
locationText.text = nil;
[[NSNotificationCenter defaultCenter] postNotificationName:@"Test1" object:self];
}
- (void)viewDidUnload
{
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
NSLog(@"%@", @"Registering for keyboard events...");
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
keyboardVisible_ = NO;
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
NSLog(@"%@", @"Unregistering for keyboard events...");
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#pragma mark -
#pragma mark Keyboard handlers
- (void) keyboardDidShow:(NSNotification *)notif {
NSLog(@"%@", @"Received UIKeyboardDidShowNotification");
if (keyboardVisible_) {
NSLog(@"%@", @"Keyboard is already visible. Ignoring notifications.");
return;
}
// The keyboard wasn't visible before
NSLog(@"Resizing smaller for keyboard");
// Get the origin of the keyboard when it finishes animating
NSDictionary *info = [notif userInfo];
NSValue *aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
// Get the top of the keyboard in view's coordinate system.
// We need to set the bottom of the scrollview to line up with it
CGRect keyboardRect = [aValue CGRectValue];
keyboardRect = [self.view convertRect:keyboardRect fromView:nil];
CGFloat keyboardTop = keyboardRect.origin.y;
// Resize the scroll view to make room for the keyboard
CGRect viewFrame = self.view.bounds;
viewFrame.size.height = keyboardTop - self.view.bounds.origin.y;
self.scrollView.frame = viewFrame;
keyboardVisible_ = YES;
}
- (void) keyboardDidHide:(NSNotification *)notif {
NSLog(@"%@", @"Received UIKeyboardDidHideNotification");
if (!keyboardVisible_) {
NSLog(@"%@", @"Keyboard already hidden. Ignoring notification.");
return;
}
// The keyboard was visible
NSLog(@"%@", @"Resizing bigger with no keyboard");
// Resize the scroll view back to the full size of our view
self.scrollView.frame = self.view.bounds;
keyboardVisible_ = NO;
}
@end
Thanks in advance :)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…