如何上传照片和照片的描述TOGETHER在iphone应用程序?

问题描述:

下面是我正在做的:允许用户使用他们的iPhone应用程序将照片和两个描述字段上传到MySQL。我已经想出了如何配置应用程序,以便上传两个描述字段中的文本(在我的Web服务器上使用PostURL和附带的.php文件)。如何上传照片和照片的描述TOGETHER在iphone应用程序?

我遇到问题的方式是如何将照片添加到混合中,并将照片和文本字段一起传输到数据库中,并将它们放入相应的列(图像,名称,消息)中。

我的头文件和实现文件应该是什么样子?作为额外的好处,我的.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 

“嗨,对不起我的英语不是很好,如果有人喜欢纠正我的新版本我将不胜感激这个”

嗨,你可以使用(获取,发布,像SOAP一样的网络服务,休息服务如json)的请求。

从我的经验,如果你想发送一个图像,你必须使用base64binary这是一个字节数组的字符串表示,因为我从来没有能够发送一个字节数组,但如果是字符串,你可以发送这个正常没有base64binary。

+0

不幸的是,这并没有回答我的问题。我正在寻找特定的代码,以解决上述问题。谢谢你的回应,NTTake。 – Mike 2012-04-23 13:57:41

+0

好吧@Mike我现在不知道如何在php中创建接收数据的后端,但如果您有后端,我可以帮助您使用iOS的前端。祝你好运。 – NTTake 2012-04-23 14:37:23

+0

我可以找出PHP,但你能帮我编写前端文件吗?例如,你能不介意我的代码应该在头文件和实现文件中看起来如何? – Mike 2012-04-23 14:59:42