2012-12-03 82 views
5

我正在使用json解析來使用web服務。我能夠從Web服務獲取圖像,有人可以幫我發佈圖像嗎?我如何將圖像發佈到Web服務?如何將圖像發佈到Web服務器

+0

你考慮使用'ASIHTTPRequest'的最好方法? – limon

+0

@mstfbsnli我是新來的iOS ...所以,請幫助我,我如何通過ASIHTTPRequest ..實現它。請.. – Shivaay

+0

這裏是[解釋](http://allseeing-i.com/ASIHTTPRequest/How - 使用)的使用 – limon

回答

2

在此處查找服務器端(PHP)編碼使用隨機名稱上傳。它也會給出圖像鏈接作爲迴應。

//Create a folder named images in your server where you want to upload the image. 
// And Create a PHP file and use below code . 


<?php 
$uploaddir = 'images/'; 
$ran = rand() ; 

$file = basename($_FILES['userfile']['name']); 
$uploadfile = $uploaddir .$ran.$file; 

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { 
     echo "www.host.com/.../images/{$uploadfile}"; 
} 
?> 

這裏是iOS的代碼

- (IBAction)uploadClicked:(id)sender 
{ 
    /* 
     turning the image into a NSData object 
     getting the image back out of the UIImageView 
     setting the quality to 90 
     */ 
     NSData *imageData = UIImageJPEGRepresentation(imageView.image, 90); 
     // setting up the URL to post to 
     NSString *urlString = @"your URL link"; 

     // setting up the request object now 
     NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
     [request setURL:[NSURL URLWithString:urlString]]; 
     [request setHTTPMethod:@"POST"]; 

     /* 
     add some header info now 
     we always need a boundary when we post a file 
     also we need to set the content type 

     You might want to generate a random boundary.. this is just the same 
     as my output from wireshark on a valid html post 
     */ 
     NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"]; 
     NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; 
     [request addValue:contentType forHTTPHeaderField: @"Content-Type"]; 

     /* 
     now lets create the body of the post 
     */ 
     NSMutableData *body = [NSMutableData data]; 
     [body appendData:[[NSString stringWithFormat:@"rn--%@rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];  
     [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name="userfile"; filename="ipodfile.jpg"rn"] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-streamrnrn"] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [body appendData:[NSData dataWithData:imageData]]; 
     [body appendData:[[NSString stringWithFormat:@"rn--%@--rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
     // setting the body of the post to the reqeust 
     [request setHTTPBody:body]; 

     // now lets make the connection to the web 
     NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
     NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 

     NSLog(returnString); 

} 
+0

我是iOS.so新手,是否有可能爲您詳細闡述一下如何做到這一點? – Shivaay

+0

查看我編輯的答案,並讓我知道你正在面臨的問題或你無法理解的哪一行。 –

+0

在我的項目中,我的服務器端不是PHP的python.so,所做的更改是什麼? – Shivaay

4

這將是沿着這行的東西...

NSMutableURLRequest *mutableRequest = [[NSMutableURLRequest alloc] initWithURL:@"you url"]; 

[mutableRequest addValue:@"image/jpeg" forHTTPHeaderField: @"Content-Type"]; 

NSMutableData *body = [NSMutableData data]; 

[body appendData:[NSData dataWithData:UIImageJPEGRepresentation(self.image, 0.9)]]; 

[mutableRequest setHTTPBody:body]; 

NSURLResponse *response; 
NSError *error; 

(void) [NSURLConnection sendSynchronousRequest:mutableRequest returningResponse:&response error:&error]; 

感謝

+0

謝謝..但在哪種方法中,我需要添加這些代碼行。 – Shivaay

+0

不確定你的意思是哪種方法?這完全取決於你編寫程序的方式。在某些時候,你會有一個名爲「uploadImage」的方法。那麼這就是你上傳圖片的方式。 – Fogmeister

+0

tnx ..我問你這樣的問題,因爲我是新的iOS.so,沒有這麼多想法abt它.. – Shivaay

2

檢查下面好tutorial.In該教程你能找到的IOS端的代碼以及服務器端PHP代碼太。 http://zcentric.com/2008/08/29/post-a-uiimage-to-the-web/

+0

tnx ..但它有必要知道服務器端..因爲我只有後的網址。 .no,服務器端信息.. – Shivaay

+0

只問服務器端開發者他已經實現了什麼,還要求他給出一個html表單來驗證他的代碼。這可能是在他身邊的錯誤,這可能會導致你的噩夢:) –

+0

是的..tnx很多..san – Shivaay