I am trying to make an upload function for images in a IOS app. But when I use the code below - then nothing is happend! It works with text, but not the image.
My image is taken with the camera, and then placed in a UIImageView.
My question is; how I get the image from the imageView into the uploade script?
- (IBAction)sendAnmeldelse:(id)sender {
NSData *imageData = UIImagePNGRepresentation(imageView.image);
NSString *filename = [NSString stringWithFormat:@"bild.png"];
NSString *urlString = @"http://gourmetkongen.dk/data/upload.php";
// setting up the request object now
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/png\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
NSLog(@"Sending succes!");
}
And here is the image in the UIImageView:
UIImage *image = info[UIImagePickerControllerOriginalImage];
imageView.image = image;
Hope someone can help me!
// And here is the PHP-code:
$uploaddir = '';
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "OK";
} else {
echo "ERROR";
}