0

这是我正在做的事情:允许用户使用他们的 iphone 应用程序将照片和照片的两个描述字段上传到 MySQL。我已经想出了如何配置应用程序,以便上传来自两个描述字段的文本(使用 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
4

1 回答 1

0

(^.^)“抱歉,我的英语不好,如果有人喜欢纠正我的编辑,我将不胜感激”

您好,您可以使用(获取、发布、soap 等 Web 服务、json 等休息服务)请求。

根据我的经验,如果您想发送图像,则必须使用 base64binary 这是字节数组的字符串表示形式,因为我从未能够发送一个字节数组,但是如果是字符串,则可以在没有 base64binary 的情况下正常发送。

于 2012-04-23T05:09:59.823 回答