dongyan6503 2017-03-20 17:22
浏览 66
已采纳

objective-c服务器上的上传图像未完成

I am maintaining some code that upload photos from an ipad onto apache2 server via objective-c and php. In objective-c the picture is updated using AFNetworking POST function. The picture actually is being uploaded successfully, however it is incomplete. Here is what I get on the server: enter image description here

As you can see it only shows the top left part of the screenshot. The code in objective-c is something like these for imagePickerController

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    LoginData * dataObject = [self dataObject];
    NSString *tempPath = [NSString stringWithFormat:@"upload-image.jpg"];
    NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:tempPath];

    UIImage *img = [info objectForKey:UIImagePickerControllerEditedImage];
    NSData *imageData = UIImageJPEGRepresentation(img, 1);
    [imageData writeToFile:path atomically:YES];

    NSString *uploaddir = [NSString stringWithFormat:@"../exampledir/"];
    NSString *requestURL = [NSString stringWithFormat:@"http://%@/handle_upload.php", httpPath];
    NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:path, @"newname",uploaddir, @"pathname", nil];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:requestURL parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:imageData name:@"uploadedfile" fileName:path mimeType:@"image/jpeg"];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [[[UIAlertView alloc]
          initWithTitle:@"Message" message:@"Upload was successful" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]show];
        NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSLog(@"%@", string);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error with upload : %@", error);
        [[[UIAlertView alloc]
          initWithTitle:@"Message" message:@"Upload was completed" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]show];
    }];
    [picker dismissViewControllerAnimated:YES completion:NULL];
}

and the file "handle_upload.php" is as followed:

<?php
require_once("validate.php");

/* This script uploads a file to a given directory with a given name
 * Parameters:
 * @pathname - the path of the directory the file will be uploaded to
 * @newname - the new name of the file
 * @uploadedfile - the file to be uploaded
 */

$target_path = $_POST['pathname'];
$new_name = $_POST['newname'];


$target_path = $target_path . basename($new_name);

if ($_FILES['uploadedfile']['error'] > 0) {
    echo "There was an error transferring the file" . PHP_EOL;
    echo "Error code: " . $_FILES['uploadedfile']['error'] . PHP_EOL;
}
if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file " . basename( $_FILES['uploadedfile']['name']) .
                     " has been uploaded";   // success is indicated by presence of string 'has been uploaded'
} else {
    echo "There was an error uploading the file, please try again!" . PHP_EOL;
    echo '$_POST[\'pathname\']: ' . $_POST['pathname'] . ' $_POST[\'newname\']: ' . $_POST['newname'] . PHP_EOL;
    echo '$target_path: ' . $target_path . PHP_EOL;

    echo "Upload: " . $_FILES["uploadedfile"]["name"] . PHP_EOL;
    echo "Type: " . $_FILES["uploadedfile"]["type"] . PHP_EOL;
    echo "Size: " . ($_FILES["uploadedfile"]["size"] / 1024) . PHP_EOL;
    echo "Stored in: " . $_FILES["uploadedfile"]["tmp_name"] . PHP_EOL;
}

?>

Have anyone every meet this kind of problem before? I will be really appreciate to any help even just some direction to solve this problem.

  • 写回答

1条回答 默认 最新

  • dousuo2812 2017-03-20 18:22
    关注

    Change UIImagePickerControllerEditedImage to UIImagePickerControllerOriginalImage

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误