【问题标题】:Adding an "Upload" button to an apps that can capture photo using iPhone's Camera将“上传”按钮添加到可以使用 iPhone 的相机拍摄照片的应用程序
【发布时间】:2013-06-12 01:35:03
【问题描述】:

我是XCode和Objective-C的新手,按照http://www.techotopia.com/index.php/An_Example_iOS_4_iPhone_Camera_Application_(Xcode_4)的例子, 这样我就有一个应用程序可以调用设备中的相机(在我的情况下,我使用的是iPod Touch)并捕获图片,然后它可以在图像视图中显示图片,或者选择相机胶卷中的图片显示在图像视图中。这在我的 iPod Touch 中经过测试并完美运行。

现在,我想添加一个上传按钮,可以将捕获的图像上传到我的服务器 (例如:http://xxx.xxx.xxx.xxx/photo/)以便我可以在我的计算机中查看图像,任何人都可以建议一种方法让我这样做吗?或者如果它不能完成,我需要设置一个基于 php 的服务器来处理这个上传问题。

【问题讨论】:

  • 如果你只想在电脑上看到你的照片,你不能把图片设置为应用内电子邮件的附件吗?
  • 因为我想在 iPod 上运行应用程序,所以我想在应用程序中上传照片,而不是电子邮件
  • @Sean你能告诉我上述问题的解决方案吗?

标签: ios camera image-uploading ipod-touch


【解决方案1】:

你想上传一张图片到php服务器,在objective-c中你可以这样做:

//To convert your image file to raw.
NSData *imageData   = UIImagePNGRepresentation(self.imageView.image);
NSString *urlString = @"http://sample.com/upload.php";
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
//Boundary and Header.
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
//Your uploading file.
NSMutableData *bodyData = [NSMutableData data];
[bodyData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
/*
 * Declares the PHP received $_FILES variables.
 * - $_FILES['_varName']['name'] = _sample.png
 */
[bodyData appendData:[@"Content-Disposition: form-data; name=\"_varName\"; filename=\"_sample.png\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

[bodyData appendData:[NSData dataWithData:imageData]];
[bodyData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:bodyData];
//Received response of server.
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
//Dumps it.
NSLog(@"%@", returnString);

希望对你有帮助。

【讨论】:

  • 首先感谢您的回复。郭明林,你帮了我大忙。但是我想问是否需要知道每张图片的名称,并且每次上传不同的图片都要修改上面的方法?
  • 有什么方法可以上传相机刚拍的图吗?
  • 任何东西,很高兴为您提供帮助,我在GitHub上为您写了一个示例,您可以尝试使用它:KRUploader
  • 感谢您再次帮助我。但是现在我在我的 Windows PC 上设置本地服务器,运行 wamp 服务器,并创建这个 php 文件,我的 iPod 说“未定义索引:上传到 C:\wamp\www\upload.php 那么,我该怎么办?
  • 你可以提供php-error的代码行,或者你可以使用一个名为var_dump()的php函数来转储$_FILES或任何东西。确认 php 运行正确。您应该尝试像这样回显 php 错误代码编号:$_FILES['uploaded']['error'].
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多