2012-12-03 18 views
5

Sto lavorando con i servizi Web utilizzando json parsing. Sono in grado di ottenere immagini dai servizi web, qualcuno può aiutarmi a pubblicare un'immagine? Come posso pubblicare un'immagine sui servizi web?come inviare un'immagine al server Web

+0

Avete considerato utilizzando 'ASIHTTPRequest'? – limon

+0

@mstfbsnli Sono nuovo per iOS ... Quindi, per favore aiutami, come posso implementarlo tramite ASIHTTPRequest..Please .. – Shivaay

+0

Ecco la [spiegazione] (http://allseeing-i.com/ASIHTTPRequest/How -uso) di utilizzo – limon

risposta

2

Trova qui la codifica serveride (PHP) per l'immagine Carica con nome casuale. inoltre fornirà il link dell'immagine come risposta.

//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}"; 
} 
?> 

e qui è il codice di 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

Sono nuovo ad iOS.so, è possibile per te elaborare un po 'come fare questo ?? – Shivaay

+0

Vedere la mia risposta modificata e fammi sapere dove stai affrontando il problema o quale linea non puoi capire. –

+0

Nel mio progetto il mio lato server non è php suo python.so, quali sono le modifiche da apportare ?? – Shivaay

4

sarà qualcosa sulla falsariga di questo ...

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]; 

Grazie

+0

grazie..ma in quale metodo devo aggiungere queste righe di codice. – Shivaay

+0

Non sei sicuro di cosa intendi quale metodo? Dipende interamente da come hai scritto il tuo programma. Ad un certo punto avrai un metodo chiamato "uploadImage" o qualcosa del genere. Bene, questo è il modo in cui carichi quell'immagine. – Fogmeister

+0

tnx .. Ti ho fatto questa domanda perché sono nuovo su iOS.so, non ho così tanta idea .. – Shivaay

2

Controlla qui sotto buona tutorial.In che tutorial in grado di trovare il codice ios lato pure codice lato server PHP pure. http://zcentric.com/2008/08/29/post-a-uiimage-to-the-web/

+0

tnx..ma è necessario sapere il lato server ... perché ho solo l'URL per la posta. .no, informazioni sul lato server .. – Shivaay

+0

Chiedi allo sviluppatore lato server che cosa ha implementato, chiedigli anche di fornire un modulo html per convalidare il suo codice. Potrebbe essere qualcosa di sbagliato nella sua parte e questo potrebbe causare il tuo incubo :) –

+0

sì..tnx un sacco..san – Shivaay

Problemi correlati