iPhone: Posting UIImage's binary data to web along with other form data
<p>I've been trying to work on this for days now. I've done a lot of research and have tried many other things, but nothing seems to work. Posted is the code that seems like it is the closest to working. All of the data populates in the database except for the image. What I am trying to gain from this is to be able to post the exact data that is returned by UIImagePNGRepresentation(image). I assume that this is binary data, but am not 100%. Any help would be appreciated. Thanks!</p>

<pre><code>-(void)sendFormData:(NSArray*)data{
// add boundary
NSString *boundary=@"----Boundary+AaB03x";

// create array of php vars
NSArray *phpVariables=[NSArray arrayWithObjects:@"username",@"picture",@"firstName",@"lastName",@"phone",@"email",@"password",nil];
// create body data
NSMutableData *body=[[NSMutableData alloc] init];

// dictionary of fields
NSMutableDictionary *formFields=[[NSMutableDictionary alloc] init];
// fill dictionary
for(int i=0; i&lt;[data count]-1; i++) [formFields setObject:[data objectAtIndex:i] forKey:[phpVariables objectAtIndex:i]];


NSMutableURLRequest *urlRequest=[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"MY_URL"]];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setValue:[NSString stringWithFormat:@"multipart/form-data, boundary=%@",boundary] forHTTPHeaderField:@"Content-Type"];

NSEnumerator *dictionaryEnumerator=[formFields keyEnumerator];
NSString *currentKey=nil;
while((currentKey=[dictionaryEnumerator nextObject])){
    id currentValue=[formFields objectForKey:currentKey];
    if([currentValue class]==[UIImage class]){
        [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary]  dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"myphoto.png\"\r\n",currentKey] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[@"Content-Type: image/x-png\r\n" dataUsingEncoding:NSASCIIStringEncoding]];
        [body appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:UIImagePNGRepresentation(currentValue)];
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    }
    else{
        [body appendData:[[NSString stringWithFormat:@"--%@\r\nContent-Disposition: form-data; name=\"%@\"\r\n\r\n%@\r\n",boundary,currentKey,currentValue] dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO]];
    }
}

[urlRequest setHTTPBody:body];

NSURLResponse *response;
NSError *error;
NSData *serverResponseData=[NSURLConnection sendSynchronousRequest:urlRequest
                                                 returningResponse:&amp;response
                                                             error:&amp;error];
[body release];
[formFields release];

NSString *serverResponseString=[[NSString alloc] initWithData:serverResponseData
                                                     encoding:NSUTF8StringEncoding];
[self performSelectorOnMainThread:@selector(showServerResponseWithString:)
                       withObject:serverResponseString
                    waitUntilDone:FALSE];
}
</code></pre>

<p><strong>UPDATE:</strong></p>

<p>The code above works fine and it was actually an issue with the PHP.</p>
