objective-c服务器上传的图片不完整
objective-c the upload image on the server is incompleted
我正在维护一些代码,通过 objective-c 和 php 将照片从 ipad 上传到 apache2 服务器。在 objective-c 中,使用 AFNetworking POST 函数更新图片。图片确实上传成功,但是不完整。这是我在服务器上得到的:
如您所见,它只显示了屏幕截图的左上部分。
objective-c 中的代码类似于 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];
}
文件"handle_upload.php"如下:
<?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;
}
?>
有没有人遇到过这种问题?我将非常感谢任何帮助,即使只是解决这个问题的一些方向。
将UIImagePickerControllerEditedImage
更改为UIImagePickerControllerOriginalImage
我正在维护一些代码,通过 objective-c 和 php 将照片从 ipad 上传到 apache2 服务器。在 objective-c 中,使用 AFNetworking POST 函数更新图片。图片确实上传成功,但是不完整。这是我在服务器上得到的:
如您所见,它只显示了屏幕截图的左上部分。 objective-c 中的代码类似于 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];
}
文件"handle_upload.php"如下:
<?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;
}
?>
有没有人遇到过这种问题?我将非常感谢任何帮助,即使只是解决这个问题的一些方向。
将UIImagePickerControllerEditedImage
更改为UIImagePickerControllerOriginalImage