我编写了一个应用程序,它拍摄照片并使用该数据库上的 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;
最佳答案
好的,回答我的问题,这就是解决它的方法:UIImageFix
我有点不愿意投入这么大的代码块来解决一个简单的问题,但它并不像我最初想象的那么容易解决。
我担心旋转的 UIImage 是否会导致在手机上读取文件时出现问题,但这不是问题,因为 Exif 数据保持完好,手机在将其呈现在 ImageView 中之前会先查看它。
作为对 scaleAndRotate 函数的补充,这就是我最后必须写的:
UIImage *imageToUpload = scaleAndRotateImage(self.theImageView.image);
关于php - 使用 ASIHTTPRequest iPhone 上传的 JPEG 图像方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5523951/