2012-04-05 5 views
0

Hallo Freunde,Wie bekomme ich die Zeichenfolge zu einer bestimmten URL?

Ich versuche, die String-Daten und Bilddaten von einem url.but der String-Daten

zeigt null.And jemand mir sagen, zu bekommen, wie das Bild auf die angegebene URL zu laden.

hier schreibe ich diesen Code

UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://192.168.1.105:9966/PC/DisplayImage?id=2"]]]; 

myimages.image=image; 
NSURL *url = [ NSURL URLWithString: [ NSString stringWithFormat:@"http://192.168.1.105:9966/PC/DisplayImage?id=2"] ]; 
NSData *imageData = UIImageJPEGRepresentation(image, 90); 
NSString *encodedImage = [Base64 encode:imageData]; 
NSLog(@"my encoding image is %@",encodedImage); 
NSURL *url = [ NSURL URLWithString: [ NSString stringWithFormat: @"http://192.168.1.105:9966/PC/DisplayImage?id=2"] ]; 
NSString *test = [NSString stringWithContentsOfURL:url]; 
NSLog(@"naveenkumar testing%@",test); 
+1

Dies ist nicht die Ursache, aber Sie brauchen nicht '[ NSString stringWithFormat: 'wenn Sie nur eine statische Zeichenfolge verwenden. Sie können einfach 'NSURL URLWithString: @" http: // 192 .... "]' –

Antwort

1

Sie können NSURLConnection verwenden für Bild hochladen per Post Methode -

NSURL *postURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@submit.php",@"http://x.com/"]]; 

    NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:postURL 
                   cachePolicy:NSURLRequestUseProtocolCachePolicy 
                  timeoutInterval:30.0]; 

    // change type to POST (default is GET) 
    [postRequest setHTTPMethod:@"POST"]; 

    NSMutableData *postBody = [NSMutableData data]; 


    // just some random text that will never occur in the body 
    NSString *stringBoundary = @"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo"; 
    // header value 
    NSString *headerBoundary = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", 
           stringBoundary]; 
    // set header 
    [postRequest addValue:headerBoundary forHTTPHeaderField:@"Content-Type"]; 
// image ------------ 

     [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [postBody appendData:[@"Content-Disposition: form-data; name=\"text\"; filename=\"frontimage.png\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
     [postBody appendData:[@"Content-Type: image/png\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
     [postBody appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
     // add it to body 

     [postBody appendData:UIImageJPEGRepresentation(image, 90)]; 
     [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
// final boundary 

    [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; 


    // add body to post 
    [postRequest setHTTPBody:postBody]; 

    NSURLConnection *theConnection = [NSURLConnection connectionWithRequest:postRequest delegate:self]; 

    if (theConnection) 
     NSLog(@"%@",theConnection); 
+0

Hallo Freund ich versuche auf diese Weise, aber das Problem ist, wie die PHP-Datei mit Hilfe von Xcode.Crate.is es möglich ist Oder direkt herunterladen Die PHP-Datei – kumar

+0

Was ist die Notwendigkeit der Erstellung von PHP-Datei über xcode. Die Datei sollte sich auf dem Server befinden, den Sie mit NSURLConnection aufrufen. – saadnib

+0

Ich habe versucht, auszuführen, aber ich habe die Verbindung nicht erhalten. – kumar