2011-04-02 48 views
1

我已經編寫了一個應用程序,它使用該數據庫上的PHP文件將照片上傳到遠程數據庫。問題是,當從網站上讀取照片或從鏈接加載照片時,圖像呈現在橫向上。 有一部分應用程序顯示這個圖像,當在應用程序中加載時方向是正確的,所以我認爲它是爲了做到這一點而獲取exif數據? 我的問題基本上是如何在上傳之前旋轉UIImage對象,或者如何更改PHP數據,以便將它放在數據庫中正確的方向?從應用使用ASIHTTPRequest iPhone上傳的JPEG圖像方向

代碼:

// 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; 
+0

什麼是'[自我更新:文件名]'在你的代碼? – Illep 2012-02-14 00:59:06

回答

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中所做的。

+1

答案的第一部分有點不被接受,至少,[居高臨下](http://meta.stackexchange.com/questions/15650/ban-lmgtfy-let-me-google-that-for-you -links/15652#15652)。第二部分似乎沒有解決這個問題,因爲我把這個問題看作是:'我怎樣才能使用/訪問EXIF數據來正確定位使用php的圖像? – 2011-04-02 22:33:58

+0

嗨,該教程看起來很有用,生病試圖找到一些工作,併發回我的結果。我真的不在乎它的exif數據是否被讀取或者只是沒有在橫向上進行。星期四得到一個項目的演示!謝謝你的幫助。 – paruss 2011-04-04 10:29:50

2

好的回答我的問題,這就是解決它的問題:UIImageFix 我有點不情願放入如此巨大的代碼塊來解決一個簡單的問題,但它不像我第一次那麼容易解決思想。 我擔心如果旋轉的UIImage會導致讀取手機上的文件時出現問題,但這並不是問題,因爲Exif數據保持完好,手機在將它呈現在圖像視圖中之前會先看到它。

這是所有我到底寫爲除scaleAndRotate功能:

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

謝謝,這對我很好。在此之前無法獲取圖像方向 – davidfrancis 2012-03-22 12:34:09