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?

3 Answers 3

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"]; 
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thats not a bad idea at all, I'll give it a go a little later today. thnx
Tried it, got the same problem.
I found the issue... my PHP settings. upload_max_filesize was set to 2M and the file was 5M. Took me 4 hours to figure that out :(
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.

Comments

0

Try this code, it works perfect for me:

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

4 Comments

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.
Let me ask our server-side programmer.
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 You read my answer correctly. That was exactly what happened, which I answered already to the question. Thanks though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.