I have made some image views and their button under them. When user select image from gallery it displays on image view. Now i want that image to be sent to server through PHP web service and use POST method. I want it to first convert it to base64 bits. Can anyone help me? My code is NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"ddMMyyyyHHmmss"];
NSData *data = UIImageJPEGRepresentation(_img1.image, 1.0);
UIImage *imagedata = [UIImage imageWithData:data];
NSString * base64String = [data base64EncodedStringWithOptions:0];
NSDate *now = [[NSDate alloc] init];
NSString *theDate = [dateFormat stringFromDate:now];
//---------- Compress photo----------
double compressionRatio = 1;
int resizeAttempts = 5;
while ([data length] > 1000 && resizeAttempts > 0)
{
resizeAttempts -= 1;
compressionRatio = compressionRatio*0.6;
data = UIImageJPEGRepresentation(imagedata,compressionRatio);
}
NSString *baseurl = @"myurl";
NSURL *dataURL = [NSURL URLWithString:baseurl];
NSMutableURLRequest *dataRqst = [NSMutableURLRequest requestWithURL:dataURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:500.0];
[dataRqst setHTTPMethod:@"POST"];
NSString *stringBoundary = @"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo";
NSString *headerBoundary = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary];
[dataRqst addValue:headerBoundary forHTTPHeaderField:@"Content-Type"];
NSMutableData *postBody = [NSMutableData data];
// -------------------- ---- image Upload Status -----------------------
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image\"; filename=\"%@\"\r\n",[NSString stringWithFormat:@"%@.jpeg",theDate]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:data];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"--%@--\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[dataRqst setHTTPBody:postBody];
self.conn = [[NSURLConnection alloc] initWithRequest:dataRqst delegate:self];
//SubmitDetailsViewController *presales = [self.storyboard instantiateViewControllerWithIdentifier:@"SubmitDetailsViewController"];
// [self.navigationController pushViewController:presales animated:YES];
}
pragma mark NSURLConnection Delegate Methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { self->recieveData = [[NSMutableData alloc] init]; }
//------------- Response from server ------------- - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [ self->recieveData appendData:data];
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&error];
}