使用ASIHTTPRequest iPhone上传的JPEG图像方向

时间:2011-04-02 15:08:39

标签: php ios ios4 uiimage rotation

我编写了一个应用程序,它使用该数据库上的PHP文件拍摄照片并将其上传到远程数据库。问题是,当从网站读取照片或刚刚从链接加载照片时,图像以横向显示。 有一部分应用程序显示此图像,在应用程序中加载时方向是正确的所以我认为它是获取exif数据,以便这样做? 我的问题基本上是如何在上传之前旋转UIImage对象,或者如何更改PHP数据以便将它以正确的方向放在数据库中?

应用代码:

// Display the activity indicator 
[myActivityIndicator startAnimating];
//Set up variables and copy image into NSData compressed
srandom(time(NULL));
NSURL *url = [NSURL URLWithString:@"http://www.mydomain/test-upload.php"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];  
NSData *compressedImageData = UIImageJPEGRepresentation(theImageView.image, 0.5f);

//set the filename to a random number

int randomNumber = (random() % 250000);
NSString* fileName = [NSString stringWithFormat:@"%d.jpg", randomNumber];

// Set the filename and upload

[request setFile:compressedImageData withFileName:fileName andContentType:@"image/jpeg" forKey:@"photo"];
[request setDelegate:self];
[self Update:fileName];

//Create a location manager and start it uploading (delegate : Location update)
[request startAsynchronous];

代码示例不适用于PHP:

$uploaddir = 'images/';
$file = basename($_FILES['photo']['name']);
$uploadfile = $uploaddir . $file;

if (move_uploaded_file($_FILES['photo']['tmp_name'], $uploadfile)) {
        echo "http://www.mysite.com/{$file}";
}
else {
$isfile = $_FILES['photo'];

if(file_exists($file)) {
echo 'file exists';
} else {
echo 'File not found!';
}

echo "fail";
echo "Name is" . $file; 

2 个答案:

答案 0 :(得分:2)

好的,回答我的问题,这就解决了它:UIImageFix 我有点不愿意用这么大的代码来解决一个简单的问题但是它并不像我最初想的那么容易解决。 我担心如果旋转的UIImage会导致手机上的文件读取出现问题,但这不是问题,因为Exif数据保持完好并且手机在图像视图中显示之前会看到它。

这是我最后必须写的作为scaleAndRotate函数的补充:

UIImage *imageToUpload = scaleAndRotateImage(self.theImageView.image);

答案 1 :(得分:1)

How to Rotate a UIImage 90 degrees?可能会帮助您以任何方向旋转图像。

如果您想在iPhone上处理exif数据,这可能会有所帮助:http://www.gotow.net/creative/wordpress/?p=64,否则:http://www.php.net/manual/en/function.exif-read-data.php#76964与我在Python中所做的相同。

相关问题