【问题标题】:How to upload photo and photo's description TOGETHER in iphone app?如何在 iphone 应用程序中一起上传照片和照片的描述?
【发布时间】:2012-04-23 04:28:19
【问题描述】:

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

【问题讨论】:

    标签: iphone mysql


    【解决方案1】:

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

    您好,您可以使用(get、post、soap 之类的 web 服务、json 之类的 rest 服务)请求。

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

    【讨论】:

    • 不幸的是,这并不能回答我的问题。我正在寻找可以修复我上面的特定代码。不过,感谢您的回复,NTTake。
    • 好的 @Mike 我现在不知道如何在 php 中制作后端以接收数据,但如果您有后端,我可以帮助您处理 iOS 的前端。祝你好运。
    • 我能搞懂PHP,但你能帮我编写前端文件的代码吗?例如,您介意向我展示我的代码在头文件和实现文件中的外观吗?
    • 嗨 @Mike 是的,我可以,但我现在必须知道如何发送类似 post、get 或 web 服务(如肥皂),这是在服务器端。
    • backupemailsmk@gmail.com 谢谢NTTake!!
    猜你喜欢
    • 1970-01-01
    • 2022-11-18
    • 2019-03-10
    • 2011-11-10
    • 2011-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多