Monday, 8 August 2011

IMAGE ( BASE-64 STRING ) POST URL IN OBJECTIVE C



Steps:

1.Add Cryptor library for encryption/decryption
2.UIImageView * imageView;

/*--------------------------------------Code Starts here-------------------------- -------------*/
     Cryptor *crypt = [[Cryptor alloc] init];
    NSData *imgData =[[NSData alloc]initWithData:UIImagePNGRepresentation(self.imageView.image)];
    NSString *imageString = (NSString *)[crypt base64Encoding:imgData];
    
    
    //Your WebMethod =>
*webMethodname 
*inputParameters -> Method expects parameter.
 *kURI-> Path where you call webservices .

    NSString *stringURI= [NSString stringWithFormat:@"%@?method=WBMETHODNAME & inputParameters=%@,kURI,inputfields];
    stringURI = [stringURI stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
    

    NSURL *url=[NSURL URLWithString:stringURI];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    
    NSString *postString = [NSString stringWithFormat:@"image=%@",[self encodeString:imageString]];
    
    NSData *postBody = [postString dataUsingEncoding:NSUTF8StringEncoding];
    
    NSString *contentType = @"application/x-www-form-urlencoded; charset=utf-8";
    [request addValue:contentType forHTTPHeaderField:@"Content-Type"];
    
    unsigned long long postLength = postBody.length;
    
    NSString *contentLength = [NSString stringWithFormat:@"%llu",postLength];
    [request addValue:contentLength forHTTPHeaderField:@"Content-Length"];
    
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:postBody];
    
    NSError *error;
    NSURLResponse *response;
    NSData *webresponse = [[NSData data] retain];
    
    webresponse = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    NSString *strXML = [[NSString alloc]initWithBytes: [webresponse bytes]
                                               length:[webresponse length] encoding:NSUTF8StringEncoding];
    NSLog(@"RESPONSE  here is web data : %@",strXML);
    
}

- (NSString *)encodeString:(NSString *)string {
    
    NSString *newString = NSMakeCollectable([(NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)string, NULL, CFSTR(":/?#[]@!$ &'()*+,;=\"<>%{}|\\^~`"), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding)) autorelease]);
    
    return newString;
    
}

No comments:

Post a Comment