Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can try this method, I have tested it and is working</p> <p>The PHP</p> <pre><code>&lt;?php $image = $_REQUEST['image']; $imageFileName = "miImage.jpg"; $binary = base64_decode($image); $file = fopen($imageFileName, 'wb'); fwrite($file, $binary); fclose($file); echo "Upload completed with success"; ?&gt; </code></pre> <p>In YourViewController.h file </p> <pre><code>@interface YourViewController : UIViewController { NSURLConnection *serverConnection; NSMutableData *returnData; } </code></pre> <p>Copy and paste this method in YourViewController.m file</p> <pre><code>- (NSString*)base64forData:(NSData*) theData { const uint8_t* input = (const uint8_t*)[theData bytes]; NSInteger length = [theData length]; static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; NSMutableData* data = [NSMutableData dataWithLength:((length + 2) / 3) * 4]; uint8_t* output = (uint8_t*)data.mutableBytes; NSInteger i; for (i=0; i &lt; length; i += 3) { NSInteger value = 0; NSInteger j; for (j = i; j &lt; (i + 3); j++) { value &lt;&lt;= 8; if (j &lt; length) { value |= (0xFF &amp; input[j]); } } NSInteger theIndex = (i / 3) * 4; output[theIndex + 0] = table[(value &gt;&gt; 18) &amp; 0x3F]; output[theIndex + 1] = table[(value &gt;&gt; 12) &amp; 0x3F]; output[theIndex + 2] = (i + 1) &lt; length ? table[(value &gt;&gt; 6) &amp; 0x3F] : '='; output[theIndex + 3] = (i + 2) &lt; length ? table[(value &gt;&gt; 0) &amp; 0x3F] : '='; } return [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; } </code></pre> <p>Now this are the lines of code to send the image</p> <pre><code>NSURL *sendURL = [NSURL URLWithString:@"http://yourdomain.com/yourphp.php"]; NSMutableURLRequest *sendRequest = [NSMutableURLRequest requestWithURL:sendURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30]; [sendRequest setHTTPMethod:@"POST"]; [sendRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; NSData *imageData = UIImageJPEGRepresentation(yourImage, 1.0); NSString *encodedString = [[self base64forData:imageData] stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"]; stringBody = [[NSString alloc] initWithFormat:@"image=%@", encodedString]; [sendRequest setHTTPBody:[stringBody dataUsingEncoding:NSUTF8StringEncoding]]; serverConnection = [[NSURLConnection alloc] initWithRequest:sendRequest delegate:self]; [serverConnection start]; </code></pre> <p>Last thing is to set the delegate methods of the server connection</p> <pre><code>- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { returnData = [[NSMutableData alloc] init]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [returnData appendData:data]; } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSString *responseString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; NSLog(@"Response: %@", responseString); if ([responseString isEqualToString:@"Upload completed with success"]) { /* Do something on success */ } else { /* Do something if not succeeded */ } } - (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { /* Do something in fail */ } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload