0

我正在尝试为 IOS 应用程序中的图像创建上传功能。但是当我使用下面的代码时 - 什么都没有发生!它适用于文本,但不适用于图像。

我的图像是用相机拍摄的,然后放在 UIImageView 中。

我的问题是;如何将 imageView 中的图像获取到 uploade 脚本中?

- (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!");
}

这是 UIImageView 中的图像:

UIImage *image = info[UIImagePickerControllerOriginalImage];
imageView.image = image;

希望可以有人帮帮我!

// 这里是 PHP 代码:

$uploaddir = '';
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    echo "OK";
} else {
    echo "ERROR";
}
4

0 回答 0