如何在iPhone应用程序中一起上传照片和照片的描述?

时间:2012-04-23 04:28:20

标签: iphone mysql

以下是我正在做的事情:允许用户使用他们的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

1 个答案:

答案 0 :(得分:0)

(^。^)“对不起我的英语不好,如果有人喜欢纠正我的修改,我会很感激”

您可以使用(获取,发布,肥皂等网络服务,json等休息服务)请求。

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