1

In a current project, I have implemented an UITableViewController that collects certain data and send it to the backend server by the users request. This all works fine and dandy.. however, I have problems uploading the image.

As soon as I get the image om the UIImagePickerController and send it, the server seems to have a problem. If I encode it as JPEG, PHP doesn't seem to understand it... if I encode it as PNG the whole request seems to get screwed up.

If I, however, select a any other image that exists in the project (e.g. some background image) the upload process works perfectly! That is the weird part. As soon as I use an image from the UIImagePickerController, it doesn't work... eventhough I do get the image (see addSubview in code, which works).

This is my code:

@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);
}

Server side:

    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();

This is my result:

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

I have tried a bunch of different things, such as:

I have not changed to e.g. MKNetworkKit since there is no time or budget for that (yet), so that is not a solution.

How can I solve this issue?

Community
  • 1
  • 1
Paul Peelen
  • 10,073
  • 15
  • 85
  • 168

3 Answers3

2

Save the selected image

- (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];
}

then use ASIFormDataRequest's setFile:forKey method to send image to server

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
if (jpgPath.length > 0) {
     [request setFile:jpgPath forKey:@"image"]; 
}
Ab'initio
  • 5,368
  • 4
  • 28
  • 40
1

In the end, my former code worked perfectly... it turned out that it were my PHP settings which were incorrect. In my php.ini file, the config value upload_max_filesize was set to the default of 2M. After changing it to 200M it worked perfectly.

Paul Peelen
  • 10,073
  • 15
  • 85
  • 168
0

Try this code, it works perfect for me:

[request addData:UIImagePNGRepresentation(image) withFileName:@"image.png" andContentType:@"multipart/form-data" forKey:@"image"];
iiFreeman
  • 5,165
  • 2
  • 29
  • 42
  • As I mentioned above, the PNG representation screws up the whole form for some reason. Using that, the PHP end doesn't seem to recieve any POST data. – Paul Peelen Mar 21 '13 at 05:46
  • Let me ask our server-side programmer. – iiFreeman Mar 23 '13 at 12:21
  • 1
    As written on the top, you experience the problem related to upload_max_filesize limitation. Error = 1 means following: "The uploaded file exceeds the upload_max_filesize directive in php.ini" Default value for this setting is 2M. Seems you try to upload more that 2M. Edit php.ini and restart php (or apache, if you use it). – Karisters Mar 23 '13 at 12:31
  • @Karisters You read my answer correctly. That was exactly what happened, which I answered already to the question. Thanks though. – Paul Peelen Mar 24 '13 at 21:00