【问题标题】:Sending UIImage from UIImagePickerController to PHP server using ASIFormDataRequest使用 ASIFormDataRequest 将 UIImage 从 UIImagePickerController 发送到 PHP 服务器
【发布时间】:2013-03-20 23:21:24
【问题描述】:

在当前项目中,我实现了一个UITableViewController,它收集某些数据并根据用户请求将其发送到后端服务器。 这一切都很好,花花公子。但是,我在上传图片时遇到了问题。

只要我得到UIImagePickerController 的图像并发送它,服务器似乎就有问题。如果我将其编码为 JPEG,PHP 似乎无法理解它……如果我将其编码为 PNG,整个请求似乎就搞砸了。

但是,如果我选择项目中存在的任何其他图像(例如一些背景图像),则上传过程会完美运行!那是奇怪的部分。一旦我使用 UIImagePickerController 中的图像,它就不起作用......即使我确实得到了图像(参见代码中的 addSubview,它有效)。

这是我的代码:

@interface ServiceViewController ()

@property (nonatomic) UIImage *image;
[...]

@end

@implementation ServiceViewController

@synthesize image = _image;
[...]

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    self.image = [[info objectForKey:UIImagePickerControllerOriginalImage] copy];

    [self.tableView reloadData];

    [self dismissModalViewControllerAnimated:YES];
}

- (void)sendToApi
{
    // Send post data
    NSString *urlString = [NSString stringWithFormat:@"%@/%@", API_BASE_DOMAIN2, @"requestservice"];
    urlString = @"http://localhost/api/public_html/requestservice";

    NSURL *url = [NSURL URLWithString:urlString];

    ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:url];
    [request setUseKeychainPersistence:YES];
    [request setPostValue:[self.problems objectAtIndex:self.problem] forKey:@"problem"];
    [request setPostValue:[self stationName] forKey:@"station"];
    [request setPostValue:self.otherInformation forKey:@"information"];
    [request setPostValue:[NSNumber numberWithInt:self.lock] forKey:@"lock"];
    [request setUploadProgressDelegate:self];
    [request setDidFinishSelector:@selector(didCompleteRequestService:)];
    [request setDidFailSelector:@selector(processFailed:)];
    [request setDelegate:self];
    [request setRequestMethod:@"POST"];

    // Upload an image
    [request addData:[NSData dataWithData:UIImageJPEGRepresentation(self.image, 0.9)] withFileName:@"img.jpg" andContentType:@"image/jpeg" forKey:@"picture"];


    UIImageView *view = [[UIImageView alloc] initWithImage:self.image];
    [view sizeToFit];
    [self.view addSubview:view];

    [request startAsynchronous];
}

- (void)setProgress:(float)newProgress {
//    [prgressView setProgress:newProgress];
    NSLog(@"New progress: %f", newProgress);
}

- (void)didCompleteRequestService:(ASIHTTPRequest *)request
{
    NSLog(@"Request completed");
    NSLog(@"Result: %@", request.responseString);
}

服务器端:

    var_dump($_FILES);

    if ($this->_aPicture !== null) {
        $sTarget = DIR_SERVICE . $this->_sFileNamePrefix . ".jpg";

        var_dump($this->_aPicture);
        var_dump($sTarget);

        if (!move_uploaded_file($this->_aPicture['tmp_name'], $sTarget)) {
            $this->returnHttpError(500);
        }
    }

    $this->_writeDataToFile();

这是我的结果:

2013-03-21 00:04:04.859 AppName[15598:c07] New progress: 0.000000
2013-03-21 00:04:04.862 AppName[15598:c07] New progress: 1.000000
2013-03-21 00:04:04.863 AppName[15598:c07] Request completed
2013-03-21 00:04:04.863 AppName[15598:c07] Result: array(1) {
  ["picture"]=>
  array(5) {
    ["name"]=>
    string(7) "img.jpg"
    ["type"]=>
    string(0) ""
    ["tmp_name"]=>
    string(0) ""
    ["error"]=>
    int(1)
    ["size"]=>
    int(0)
  }
}
array(5) {
  ["name"]=>
  string(7) "img.jpg"
  ["type"]=>
  string(0) ""
  ["tmp_name"]=>
  string(0) ""
  ["error"]=>
  int(1)
  ["size"]=>
  int(0)
}
string(58) "/Users/paulp/Sites/api/service/K98XQPo7zx.jpg"
Sending error: 500

我尝试了很多不同的东西,例如:

我没有更改为例如MKNetworkKit,因为(还)没有时间或预算,所以这不是一个解决方案。

我该如何解决这个问题?

【问题讨论】:

    标签: php ios objective-c uiimagepickercontroller asihttprequest


    【解决方案1】:

    保存选中的图片

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
    
        //Save selected image to document directory
        jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.jpg"];
        [UIImageJPEGRepresentation(img, 1.0) writeToFile:jpgPath atomically:YES];
        [picker dismissModalViewControllerAnimated:YES];
    }
    

    然后使用ASIFormDataRequestsetFile:forKey方法将图片发送到服务器

    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    if (jpgPath.length > 0) {
         [request setFile:jpgPath forKey:@"image"]; 
    }
    

    【讨论】:

    • 这不是一个坏主意,我今天稍后再试一试。谢谢
    • 试过了,同样的问题。
    • 我发现了问题...我的 PHP 设置。 upload_max_filesize 设置为 2M,文件为 5M。我花了 4 个小时才弄清楚:(
    【解决方案2】:

    最后,我以前的代码完美运行... 原来是我的 PHP 设置不正确。在我的php.ini 文件中,配置值upload_max_filesize 设置为默认值2M。将其更改为200M 后,它运行良好。

    【讨论】:

      【解决方案3】:

      试试这个代码,它非常适合我:

      [request addData:UIImagePNGRepresentation(image) withFileName:@"image.png" andContentType:@"multipart/form-data" forKey:@"image"];
      

      【讨论】:

      • 正如我上面提到的,PNG 表示由于某种原因搞砸了整个表单。使用它,PHP 端似乎没有收到任何 POST 数据。
      • 让我问问我们的服务器端程序员。
      • 如上面所写,您遇到了与upload_max_filesize 限制相关的问题。 Error = 1 表示以下:“上传的文件超出了 php.ini 中的 upload_max_filesize 指令” 此设置的默认值为 2M。看来您尝试上传更多的2M。编辑 php.ini 并重新启动 php(或 apache,如果你使用它)。
      • @Karisters 您正确阅读了我的答案。这正是发生的事情,我已经回答了这个问题。不过还是谢谢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-24
      • 1970-01-01
      • 2012-07-19
      • 2011-07-28
      相关资源
      最近更新 更多