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:
- How to upload image that was taken from UIImagePickerController
- Upload picture selected using UIImagePickerController and some more.
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?