私が行っていることは次のとおりです。ユーザーがiPhoneアプリを使用して写真と写真の2つの説明フィールドをMySQLにアップロードできるようにします。2つの説明フィールドからのテキストがアップロードされるようにアプリを構成する方法をすでに理解しました(PostURLとそれに付随するWebサーバー上の.phpファイルを使用)。
私が問題に直面しているのは、写真をミックスに追加し、写真とテキストのフィールドをデータベースの対応する列(画像、名前、メッセージ)に一緒に送信する方法です。
ヘッダーファイルと実装ファイルはどのようになりますか?そして、追加のボーナスとして、私の.phpファイルはどのように見えるべきですか?現在の状況は次のとおりです。参考までに、これは写真ではなくテキストの送信にのみ機能します。
ヘッダーファイル:
#import <UIKit/UIKit.h>
#define kPostURL @"http://www.example.com/upload.php"
#define kName @"name"
#define kMessage @"message"
@interface FirstViewController : UIViewController<UINavigationControllerDelegate, UIImagePickerControllerDelegate>{
IBOutlet UITextField *nameText;
IBOutlet UITextView *messageText;
NSURLConnection *postConnection;
UIImageView * theimageView;
UIButton * choosePhoto;
UIButton * takePhoto;
}
@property (nonatomic, retain) IBOutlet UITextField * nameText;
@property (nonatomic, retain) IBOutlet UITextView * messageText;
@property (nonatomic, retain) NSURLConnection * postConnection;
@property (nonatomic, retain) IBOutlet UIImageView * theimageView;
@property (nonatomic, retain) IBOutlet UIButton * choosePhoto;
@property (nonatomic, retain) IBOutlet UIButton * takePhoto;
-(IBAction) getPhoto:(id) sender;
-(void) postMessage:(NSString*) message withName:(NSString *) name;
-(IBAction)post:(id)sender;
@end
実装ファイル:
#import "FirstViewController.h"
@implementation FirstViewController
@synthesize nameText, messageText, postConnection, theimageView, choosePhoto, takePhoto,postData;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void) postMessage:(NSString*) message withName:(NSString *) name {
if (name != nil && message != nil){
NSMutableString *postString = [NSMutableString stringWithString:kPostURL];
[postString appendString:[NSString stringWithFormat:@"?%@=%@", kName, name]];
[postString appendString:[NSString stringWithFormat:@"&%@=%@", kMessage, message]];
[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:messageText.text withName:nameText.text];
[messageText resignFirstResponder];
messageText.text = nil;
nameText.text = nil;
[[NSNotificationCenter defaultCenter] postNotificationName:@"Test1" object:self];
}
-(IBAction) getPhoto:(id) sender {
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
if((UIButton *) sender == choosePhoto) {
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
} else {
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
[self presentModalViewController:picker animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
theimageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end