简体   繁体   English

如何在iPhone应用程序中一起上传照片和照片说明?

[英]How to upload photo and photo's description TOGETHER in iphone app?

Here is what I am doing: Allowing the user to upload a photo and two description fields of the photo to MySQL using their iphone app. 我正在做的是:允许用户使用其iPhone应用程序将照片和照片的两个描述字段上载到MySQL。 I have already figured out how to configure the app so that the text from the two description fields are uploaded (using PostURL and an accompanying .php file on my web server). 我已经弄清楚了如何配置应用程序,以便上传两个描述字段中的文本(在我的Web服务器上使用PostURL和随附的.php文件)。

Where I am running into problems is how to add a photo into the mix, and have the photo AND text fields transmit together into the database into their corresponding columns (image, name, message). 我遇到问题的地方是如何将照片添加到混合中,并使照片和文本字段一起传输到数据库中相应的列(图像,名称,消息)中。

What should my header and implementation files look like? 我的标头和实现文件应该是什么样的? And as an added bonus, what should my .php file look like? 另外,我的.php文件应该是什么样的? Here is how they exist currently, and as an FYI, this only works to transmit text, not the photo. 这是目前它们的存在方式,作为一种仅供参考,它仅用于传输文本,而不用于传输照片。

Header file: 头文件:

#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

Implementation file: 实施文件:

#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

(^.^)"Hi sorry for my English is not good if someone like correct my redaction I would appreciate this" (^。^)“对不起,如果有人喜欢我的修改,我会很抱歉,我将不胜感激”

Hi you can use (get, post, web services like soap, rest services like json) request. 嗨,您可以使用(获取,发布,肥皂等Web服务,JSON等其他服务)请求。

From my experience if you like to send an image you have to use base64binary this is a string representation of array of bytes because I've never been able to send one array of bytes, but if is string you can send this normal without base64binary. 根据我的经验,如果您要发送图像,则必须使用base64binary,这是字节数组的字符串表示形式,因为我从未能够发送过一个字节数组,但是如果是string,则可以在不使用base64binary的情况下发送此常规消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM