HashValue mis match in PayUMoney framework, iOS - Objective-C - ios
I got error like Hash value Mismatch from PayUMoney payment gateway
Here is my code which I did, please let me know where is issue
PUMTxnParam *txnParam= [[PUMTxnParam alloc] init];
//Set the parameters
txnParam.phone = [[NSUserDefaults standardUserDefaults] valueForKey:#"mobile"];
txnParam.email = [[NSUserDefaults standardUserDefaults] valueForKey:#"username"];
txnParam.amount = _txtCash.text;
txnParam.environment = PUMEnvironmentProduction;
txnParam.firstname = [[NSUserDefaults standardUserDefaults] valueForKey:#"name1"];
txnParam.key = #"xxxxxxxx";
txnParam.merchantid = #"xxxxxxx";
txnParam.txnID = [txnData valueForKey:#"txnid"];
txnParam.surl = #"https://google.com/";
txnParam.furl = #"https://google.com/";
txnParam.productInfo = #"My App";
txnParam.udf1 = #"";
txnParam.udf2 = #"";
txnParam.udf3 = #"";
txnParam.udf4 = #"";
txnParam.udf5 = #"";
txnParam.hashValue = [self createSHA512:[NSString stringWithFormat:#"%#|%#|%#|%#|%#|%#|%#|%#|%#|%#|%#||||||%#|",txnParam.key,txnParam.txnID,txnParam.amount,txnParam.productInfo,txnParam.firstname,txnParam.email,txnParam.udf1,txnParam.udf2,txnParam.udf3,txnParam.udf4,txnParam.udf5,#"dxltF25QEp"]];
-(NSString *)createSHA512:(NSString *)string
{
const char *cstr = [string cStringUsingEncoding:NSUTF8StringEncoding];
NSData *data = [NSData dataWithBytes:cstr length:string.length];
uint8_t digest[CC_SHA512_DIGEST_LENGTH];
CC_SHA512(data.bytes, (CC_LONG)data.length, digest);
NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA512_DIGEST_LENGTH * 2];
for(int i = 0; i < CC_SHA512_DIGEST_LENGTH; i++)
[output appendFormat:#"%02x", digest[i]];
return output;
}
Please help to clear this out.
Cocoa pods and Github have their old libraries,
I just copied the library from their example which is in GitHub and it works fine :)
Related
How to force cast and convert datatype in objective c
Im a swift developer with no knowledge in objective c. Can any one help me with converting the below code in Objective C let newVersion = Int((userInfo.value(forKey: "gcm.notification.version") as string).replacingOccurrences(of: ".", with: "")) let currentVersion = Int((userDefaults.standard.value(forKey: "currentAppVersion") as string).replacingOccurrences(of: ".", with: "")) if(newVersion > currentVersion) { //code here }
NSString *newVersion = [(NSString *)[notification.userInfo valueForKey:#"gcm.notification.version"] stringByReplacingOccurrencesOfString:#"." withString:#""]; NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults]; NSString *oldVersion = [(NSString *)[standardDefaults valueForKey:#"currentAppVersion"] stringByReplacingOccurrencesOfString:#"." withString:#""]; if ([newVersion integerValue] > [oldVersion integerValue]){ //code here }
Something like the following should do: NSDictionary *userInfo; // Assuming this is a dictionary NSString *newVersionString = userInfo[#"gcm.notification.version"]; newVersionString = [newVersionString stringByReplacingOccurrencesOfString:#"." withString:#""]; NSInteger newVersion = [[[NSDecimalNumber alloc] initWithString:newVersionString] integerValue]; NSString *currentVersionString = [[NSUserDefaults standardUserDefaults] valueForKey:#"currentAppVersion"]; currentVersionString = [currentVersionString stringByReplacingOccurrencesOfString:#"." withString:#""]; NSInteger currentVersion = [[[NSDecimalNumber alloc] initWithString:currentVersionString] integerValue]; if(newVersion > currentVersion) { //code here } But this is not how you compare versions. The problem now is that for instance 1.11.1 is equal to 11.1.1. Or in other words reposition dots anyway you can but if number of digits and their order stay the same then you will detect it as a same version. You should do this per component (both in Swift or Objective-C): NSDictionary *userInfo; NSArray<NSString *> *newComponents = [userInfo[#"gcm.notification.version"] componentsSeparatedByString:#"."]; NSString *currentVersionString = [[NSUserDefaults standardUserDefaults] valueForKey:#"currentAppVersion"]; NSArray<NSString *> *currentComponents = [currentVersionString componentsSeparatedByString:#"."]; BOOL newVersionIsGreater = NO; if(newComponents.count != currentComponents.count) newVersionIsGreater = newComponents.count > currentComponents.count; else { for(int i=0; i<newComponents.count; i++) { NSInteger newInteger = [[[NSDecimalNumber alloc] initWithString:newComponents[i]] integerValue]; NSInteger currentInteger = [[[NSDecimalNumber alloc] initWithString:currentComponents[i]] integerValue]; if(newInteger != currentInteger) { newVersionIsGreater = newInteger > currentInteger; break; } } } if(newVersionIsGreater) { //code here } This now checks if we have same number of components in both cases and then iterates through them. The first component that differs will report a change.
NSString from NSData is incomplete
When I use [[NSString alloc] initWithData:subdata encoding:encoding] method , I find the string is incomplete. Why? Any restrictions in NSString? Then,I suspect that the data is too large,so I create a new method to Transfer. -(NSString *)stringFromData:(NSData *)data encoding:(NSStringEncoding)encoding{ NSInteger DataLen = data.length; NSInteger segLen = 5000; NSInteger times = DataLen/segLen; NSInteger fristLen = DataLen%segLen; NSMutableString *str = [NSMutableString string]; [str appendString:[[NSString alloc] initWithData:[data subdataWithRange:NSMakeRange(0, fristLen)] encoding:encoding]]; NSLog(#"%#",str); while (times--) { NSData *subdata = [data subdataWithRange:NSMakeRange(fristLen, segLen)] ; if (subdata) { NSString*substr = [[NSString alloc] initWithData:subdata encoding:encoding]; //tag1 NSLog(#"%#",substr); if (substr) { [str appendString:substr]; } fristLen += segLen; }else{ break; } } return str; } but in tag1, I find the string is null in some conditions. What is the problem?
How do I convert a byte array to UIImage
I have a webservice which sends me back an array of bytes like this: <ax23:IMGBLOB>-119,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,0,0,100,0,0,0,106,8,3,0,0,0,125,54,4,22,0,0,0,51,80,76,84,69,0,0,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-73,-107,-30,-126,0,0,0,16,116,82,78,83,0,16,32,48,64,80,96,112,-128,-112,-96,-80,-64,-48,-32,-16,84,-32,-88,-56,0,0,2,126,73,68,65,84,120,-38,-19,90,65,-110,-124,32,12,4,68,64,69,-32,-1,-81,-35,-53,-78,-96,40,4,-79,-70,118,-86,-90,-113,51,106,-101,116,-120,33,-127,-3,43,72,101,-84,13,127,-80,-42,40,-7,42,-63,100,108,-72,-124,53,-45,75,38,-84,62,84,-32,-41,97,-125,-72,118,-95,9,-89,-7,0,-123,-56,-115,-88,-101,35,-98,90,-79,-122,14,-84,-113,-84,49,62,116,-63,-101,126,-71,-9,112,-58,-66,25,45,127,-95,-51,118,113,-127,-20,52,-29,124,-1,34,121,-31,78,-71,-100,-119,76,-113,-32,-5,-111,65,-117,-37,43,-11,-23,82,65,118,-107,63,40,42,91,-21,-24,-96,12,-47,101,-22,64,33,8,-122,31,104,20,-123,99,9,9,-106,104,-67,-80,-7,123,-79,38,-78,-73,114,51,35,99,118,29,44,-21,-45,-27,-59,-13,59,-87,-66,-14,-118,117,66,-91,120,89,104,-102,-69,7,57,124,114,20,-11,101,-118,119,-2,40,-37,-91,69,35,111,99,-60,-25,28,99,44,-2,46,46,-9,-100,99,-108,101,-81,-28,-85,-56,49,-50,98,-86,-126,56,-50,6,-64,93,41,75,-23,44,63,13,86,29,-2,-34,97,38,69,-33,32,-44,-83,-61,-72,-89,-26,4,122,102,-14,-68,-8,-93,16,100,80,-106,-14,-123,69,52,113,102,47,96,-114,79,19,-105,-122,88,-10,10,-74,43,83,-72,-49,-87,-57,33,-82,84,-47,45,-43,-71,-115,5,92,-97,-10,-102,37,-72,104,72,-43,126,111,120,-81,41,46,91,-20,-51,-16,-115,-9,-88,94,83,100,-15,-53,116,79,18,97,37,113,-35,23,-17,-19,99,30,-72,5,-79,-24,45,-77,-108,63,-77,-22,54,9,-67,-24,-43,39,-17,-104,40,123,-101,-124,94,-58,-120,83,2,-77,77,111,-79,112,-126,-99,-88,-2,-78,-57,39,24,2,73,-62,-62,-119,53,-5,49,-128,101,23,73,-16,21,9,-53,-57,-86,72,73,35,73,112,-110,17,-18,81,-71,93,59,-103,36,-63,-118,-74,40,38,-41,125,35,-112,-108,48,-68,-107,-118,109,78,98,-70,72,8,-27,-84,73,36,-55,119,68,18,-54,86,-79,-44,-102,16,92,44,84,-79,93,74,35,7,73,74,-97,1,72,66,-104,-21,36,60,102,-78,33,18,-53,10,8,8,9,-64,93,31,41,-4,90,13,97,-56,98,-124,-92,21,72,-126,28,77,-11,27,37,-43,67,62,90,-112,-49,47,-94,-112,24,40,-119,4,-71,36,-126,20,119,-112,50,21,89,112,-89,-51,68,-101,-60,78,-99,91,7,-56,38,8,-79,-99,-125,108,76,-79,91,-20,-9,-101,5,54,122,6,-46,-10,64,52,112,32,-83,40,68,83,13,-47,30,-124,52,58,17,45,91,68,-13,25,-46,70,71,12,4,16,-93,13,-64,-112,102,37,-113,-101,-74,-66,113,-45,22,18,7,98,112,6,29,1,-30,-121,-103,-8,-79,108,57,96,118,-75,1,-77,43,6,-52,-128,81,57,116,-24,-113,63,-66,-128,63,-120,-127,63,82,66,63,28,99,56,-2,-104,15,-2,-64,-46,-8,-47,-85,47,-66,-8,-30,-77,-16,3,-74,-58,-81,-89,-116,-22,56,-74,0,0,0,0,73,69,78,68,-82,66,96,-126,####</ax23:IMGBLOB> This array of bytes are used in Java for obtaining an image, I've been trying with several methods without success, I will post one of them below: -(UIImageView *)convierteImagen: (NSMutableString *)cadenaImagen{ //NSString to NSData (temporary) NSData *tempData = [cadenaImagen dataUsingEncoding:NSUTF8StringEncoding]; //NSLog(#"datos crudos: %#", tempData); //2.NSData to Bytes const void *bytes2 = [tempData bytes]; int8_t *bytes = (int8_t*)bytes2; int len = tempData.length; cadenaImagen = [[NSMutableString alloc]init]; [cadenaImagen appendString:#"["]; for (int i = 0; i < len; i++) { if (i) [cadenaImagen appendString:#","]; [cadenaImagen appendFormat:#"%d", bytes[i]]; } [cadenaImagen appendString:#"]"]; //3.Bytes to NSData NSData *datos2 = [NSData dataWithBytes:bytes length:len]; return [[UIImageView alloc]initWithImage:[UIImage imageWithData:datos2]]; } and the way I call this method is: - (void)viewDidLoad { [super viewDidLoad]; NSMutableString *imagen = [[NSMutableString alloc] initWithString:#"-119,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,0,0,100,0,0,0,106,8,3,0,0,0,125,54,4,22,0,0,0,51,80,76,84,69,0,0,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-73,-107,-30,-126,0,0,0,16,116,82,78,83,0,16,32,48,64,80,96,112,-128,-112,-96,-80,-64,-48,-32,-16,84,-32,-88,-56,0,0,2,126,73,68,65,84,120,-38,-19,90,65,-110,-124,32,12,4,68,64,69,-32,-1,-81,-35,-53,-78,-96,40,4,-79,-70,118,-86,-90,-113,51,106,-101,116,-120,33,-127,-3,43,72,101,-84,13,127,-80,-42,40,-7,42,-63,100,108,-72,-124,53,-45,75,38,-84,62,84,-32,-41,97,-125,-72,118,-95,9,-89,-7,0,-123,-56,-115,-88,-101,35,-98,90,-79,-122,14,-84,-113,-84,49,62,116,-63,-101,126,-71,-9,112,-58,-66,25,45,127,-95,-51,118,113,-127,-20,52,-29,124,-1,34,121,-31,78,-71,-100,-119,76,-113,-32,-5,-111,65,-117,-37,43,-11,-23,82,65,118,-107,63,40,42,91,-21,-24,-96,12,-47,101,-22,64,33,8,-122,31,104,20,-123,99,9,9,-106,104,-67,-80,-7,123,-79,38,-78,-73,114,51,35,99,118,29,44,-21,-45,-27,-59,-13,59,-87,-66,-14,-118,117,66,-91,120,89,104,-102,-69,7,57,124,114,20,-11,101,-118,119,-2,40,-37,-91,69,35,111,99,-60,-25,28,99,44,-2,46,46,-9,-100,99,-108,101,-81,-28,-85,-56,49,-50,98,-86,-126,56,-50,6,-64,93,41,75,-23,44,63,13,86,29,-2,-34,97,38,69,-33,32,-44,-83,-61,-72,-89,-26,4,122,102,-14,-68,-8,-93,16,100,80,-106,-14,-123,69,52,113,102,47,96,-114,79,19,-105,-122,88,-10,10,-74,43,83,-72,-49,-87,-57,33,-82,84,-47,45,-43,-71,-115,5,92,-97,-10,-102,37,-72,104,72,-43,126,111,120,-81,41,46,91,-20,-51,-16,-115,-9,-88,94,83,100,-15,-53,116,79,18,97,37,113,-35,23,-17,-19,99,30,-72,5,-79,-24,45,-77,-108,63,-77,-22,54,9,-67,-24,-43,39,-17,-104,40,123,-101,-124,94,-58,-120,83,2,-77,77,111,-79,112,-126,-99,-88,-2,-78,-57,39,24,2,73,-62,-62,-119,53,-5,49,-128,101,23,73,-16,21,9,-53,-57,-86,72,73,35,73,112,-110,17,-18,81,-71,93,59,-103,36,-63,-118,-74,40,38,-41,125,35,-112,-108,48,-68,-107,-118,109,78,98,-70,72,8,-27,-84,73,36,-55,119,68,18,-54,86,-79,-44,-102,16,92,44,84,-79,93,74,35,7,73,74,-97,1,72,66,-104,-21,36,60,102,-78,33,18,-53,10,8,8,9,-64,93,31,41,-4,90,13,97,-56,98,-124,-92,21,72,-126,28,77,-11,27,37,-43,67,62,90,-112,-49,47,-94,-112,24,40,-119,4,-71,36,-126,20,119,-112,50,21,89,112,-89,-51,68,-101,-60,78,-99,91,7,-56,38,8,-79,-99,-125,108,76,-79,91,-20,-9,-101,5,54,122,6,-46,-10,64,52,112,32,-83,40,68,83,13,-47,30,-124,52,58,17,45,91,68,-13,25,-46,70,71,12,4,16,-93,13,-64,-112,102,37,-113,-101,-74,-66,113,-45,22,18,7,98,112,6,29,1,-30,-121,-103,-8,-79,108,57,96,118,-75,1,-77,43,6,-52,-128,81,57,116,-24,-113,63,-66,-128,63,-120,-127,63,82,66,63,28,99,56,-2,-104,15,-2,-64,-46,-8,-47,-85,47,-66,-8,-30,-77,-16,3,-74,-58,-81,-89,-116,-22,56,-74,0,0,0,0,73,69,78,68,-82,66,96,-126"]; [self.view addSubview:[self convierteImagen:imagen]]; } Any help I will appreciate :)
try this: -(UIImageView *)convierteImagen: (NSMutableString *)cadenaImagen{ NSArray *strings = [cadenaImagen componentsSeparatedByString:#","]; unsigned c = strings.count; uint8_t *bytes = malloc(sizeof(*bytes) * c); unsigned i; for (i = 0; i < c; i++) { NSString *str = [strings objectAtIndex:i]; int byte = [str intValue]; bytes[i] = (uint8_t)byte; } NSData *datos = [NSData dataWithBytes:bytes length:c]; UIImage *image = [UIImage imageWithData:datos]; return [[UIImageView alloc]initWithImage:image]; } on viewdidLoad is the same code you put: - (void)viewDidLoad { [super viewDidLoad]; NSMutableString *imagen = [[NSMutableString alloc] initWithString:#"-119,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,0,0,100,0,0,0,106,8,3,0,0,0,125,54,4,22,0,0,0,51,80,76,84,69,0,0,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-73,-107,-30,-126,0,0,0,16,116,82,78,83,0,16,32,48,64,80,96,112,-128,-112,-96,-80,-64,-48,-32,-16,84,-32,-88,-56,0,0,2,126,73,68,65,84,120,-38,-19,90,65,-110,-124,32,12,4,68,64,69,-32,-1,-81,-35,-53,-78,-96,40,4,-79,-70,118,-86,-90,-113,51,106,-101,116,-120,33,-127,-3,43,72,101,-84,13,127,-80,-42,40,-7,42,-63,100,108,-72,-124,53,-45,75,38,-84,62,84,-32,-41,97,-125,-72,118,-95,9,-89,-7,0,-123,-56,-115,-88,-101,35,-98,90,-79,-122,14,-84,-113,-84,49,62,116,-63,-101,126,-71,-9,112,-58,-66,25,45,127,-95,-51,118,113,-127,-20,52,-29,124,-1,34,121,-31,78,-71,-100,-119,76,-113,-32,-5,-111,65,-117,-37,43,-11,-23,82,65,118,-107,63,40,42,91,-21,-24,-96,12,-47,101,-22,64,33,8,-122,31,104,20,-123,99,9,9,-106,104,-67,-80,-7,123,-79,38,-78,-73,114,51,35,99,118,29,44,-21,-45,-27,-59,-13,59,-87,-66,-14,-118,117,66,-91,120,89,104,-102,-69,7,57,124,114,20,-11,101,-118,119,-2,40,-37,-91,69,35,111,99,-60,-25,28,99,44,-2,46,46,-9,-100,99,-108,101,-81,-28,-85,-56,49,-50,98,-86,-126,56,-50,6,-64,93,41,75,-23,44,63,13,86,29,-2,-34,97,38,69,-33,32,-44,-83,-61,-72,-89,-26,4,122,102,-14,-68,-8,-93,16,100,80,-106,-14,-123,69,52,113,102,47,96,-114,79,19,-105,-122,88,-10,10,-74,43,83,-72,-49,-87,-57,33,-82,84,-47,45,-43,-71,-115,5,92,-97,-10,-102,37,-72,104,72,-43,126,111,120,-81,41,46,91,-20,-51,-16,-115,-9,-88,94,83,100,-15,-53,116,79,18,97,37,113,-35,23,-17,-19,99,30,-72,5,-79,-24,45,-77,-108,63,-77,-22,54,9,-67,-24,-43,39,-17,-104,40,123,-101,-124,94,-58,-120,83,2,-77,77,111,-79,112,-126,-99,-88,-2,-78,-57,39,24,2,73,-62,-62,-119,53,-5,49,-128,101,23,73,-16,21,9,-53,-57,-86,72,73,35,73,112,-110,17,-18,81,-71,93,59,-103,36,-63,-118,-74,40,38,-41,125,35,-112,-108,48,-68,-107,-118,109,78,98,-70,72,8,-27,-84,73,36,-55,119,68,18,-54,86,-79,-44,-102,16,92,44,84,-79,93,74,35,7,73,74,-97,1,72,66,-104,-21,36,60,102,-78,33,18,-53,10,8,8,9,-64,93,31,41,-4,90,13,97,-56,98,-124,-92,21,72,-126,28,77,-11,27,37,-43,67,62,90,-112,-49,47,-94,-112,24,40,-119,4,-71,36,-126,20,119,-112,50,21,89,112,-89,-51,68,-101,-60,78,-99,91,7,-56,38,8,-79,-99,-125,108,76,-79,91,-20,-9,-101,5,54,122,6,-46,-10,64,52,112,32,-83,40,68,83,13,-47,30,-124,52,58,17,45,91,68,-13,25,-46,70,71,12,4,16,-93,13,-64,-112,102,37,-113,-101,-74,-66,113,-45,22,18,7,98,112,6,29,1,-30,-121,-103,-8,-79,108,57,96,118,-75,1,-77,43,6,-52,-128,81,57,116,-24,-113,63,-66,-128,63,-120,-127,63,82,66,63,28,99,56,-2,-104,15,-2,-64,-46,-8,-47,-85,47,-66,-8,-30,-77,-16,3,-74,-58,-81,-89,-116,-22,56,-74,0,0,0,0,73,69,78,68,-82,66,96,-126"]; [self.view addSubview:[self convierteImagen: imagen]]; } this is what I got with your byte array: so I think this should solve your problem ;)
Sending more than 2 images via email with skpsmtpmessage in ios6
I am trying to attach images to the email and send the email to my email add. The problem is that when i send out an email with 4 or 5 images attached, the app keeps processing for ever and eventually gets hanged and crashes and doesn't send the email. It is working fine with one image. I am using skpsmtp to send the mail. The same app is working fine in iOS5, but it hangs when I run it on iOS6, I am unable to send an mail as it hangs. The code looks like this: - (IBAction) SendEmail { //supports multiple emails BOOL bIsEmailValid = NO; if ([txtTO.text rangeOfString:#","].location != NSNotFound) { NSArray *arrEmails = [txtTO.text componentsSeparatedByString:#","]; DLog(#"Emails: %#", arrEmails); if ([arrEmails count] > 0) { for (int ctr = 0; ctr < [arrEmails count] ; ctr++) { NSString *strEmail = [(NSString*)[arrEmails objectAtIndex:ctr] stringByReplacingOccurrencesOfString:#" " withString:#""]; if ([self IsValidEmail:strEmail]) { bIsEmailValid = YES; } else { bIsEmailValid = NO; break; } } } } else { // only 1 email entered if ([self IsValidEmail:txtTO.text]) { bIsEmailValid = YES; } else { bIsEmailValid = NO; } } if (bIsEmailValid) { [NSThread detachNewThreadSelector:#selector(ActivityViewLoading) toTarget:self withObject:nil]; SKPSMTPMessage *testMsg = [[SKPSMTPMessage alloc] init]; testMsg.fromEmail = FROM_EMAIL; testMsg.toEmail = txtTO.text; testMsg.relayHost = RELAY_HOST; testMsg.requiresAuth = YES; testMsg.login = EMAIL_LOGIN; testMsg.pass = PASSWORD; testMsg.subject = txtSubj.text; testMsg.wantsSecure = YES; testMsg.delegate = self; testMsg.bccEmail = BCC_EMAIL; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSString *defStrName = [defaults stringForKey:#"keyEName"]; NSString *defStrContact = [defaults stringForKey:#"keyEContNo"]; NSString *defStrEmail = [defaults stringForKey:#"keyEEmailAdd"]; NSString *defStrDescr = [defaults stringForKey:#"keyEShortMessage"]; DLog(#"%# %# %# %#",defStrName, defStrContact, defStrEmail, defStrDescr); NSMutableArray* parts = [[NSMutableArray alloc] init]; NSString *strhtml = [NSString stringWithFormat:#"<div>%#</div>",[txtDesc.text stringWithNewLinesAsBRs]]; NSString *defStrWebLinks = [defaults stringForKey:#"keyEwebLinks"]; NSArray *array = [defStrWebLinks componentsSeparatedByString:#"|"]; NSString *strTemp1 = [[array objectAtIndex:0] stringByReplacingOccurrencesOfString:#" " withString:#""]; NSString *strTemp2 = [[array objectAtIndex:1] stringByReplacingOccurrencesOfString:#" " withString:#""]; NSString *strFormat1 = [NSString stringWithFormat:#"<a href=\"\%#\"\>%#</a>", strTemp1,strTemp1]; NSString *strFormat12 = [NSString stringWithFormat:#"<a href=\"\%#\"\>%#</a>", strTemp2,strTemp2]; NSString *strComb = [NSString stringWithFormat:#"%# | %#",strFormat1,strFormat12]; strhtml = [strhtml stringByReplacingOccurrencesOfString:defStrWebLinks withString:strComb]; //DLog(#"%#",strhtml); NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:#"text/html",kSKPSMTPPartContentTypeKey, strhtml,kSKPSMTPPartMessageKey,#"8bit",kSKPSMTPPartContentTransferEncodingKey,nil]; [parts addObject:plainPart]; for (int nCtr = 0; nCtr < [arrPix count]; nCtr++) { UIImageView *imageV = [arrPix objectAtIndex:nCtr]; if (imageV.image) { NSData *imageData = UIImagePNGRepresentation(imageV.image); NSString *strFileName = [NSString stringWithFormat:#"MyPicture-%d.jpeg",nCtr]; NSString *strFormat = [NSString stringWithFormat:#"image/jpeg;\r\n\tx-unix-mode=0644;\r\n\tname=\"%#\"",strFileName]; NSString *strFormat2 = [NSString stringWithFormat:#"attachment;\r\n\tfilename=\"%#\"",strFileName]; NSDictionary *vcfPart = [NSDictionary dictionaryWithObjectsAndKeys:strFormat,kSKPSMTPPartContentTypeKey, strFormat2,kSKPSMTPPartContentDispositionKey,[imageData encodeBase64ForData],kSKPSMTPPartMessageKey,#"base64",kSKPSMTPPartContentTransferEncodingKey,nil]; [parts addObject:vcfPart]; } } testMsg.parts = parts; [testMsg send]; } Need some guidance. Thanks..
Change the image format from PNG to JPEG. Hence change this line NSData *imageData = UIImagePNGRepresentation(imageV.image); to NSData *imageData = UIImageJPEGRepresentation(imageV.image,0.9); It will solve the problem...
How do I convert an LDAP "jpegPhoto" to NSString to UIImageView
I am trying to pull an LDAP "jpegPhoto" attribute from an openLDAP server using a iOS openLDAP framework. The framework pulls the data as a dictionary of NSStrings. I need to convert the NSString of "jpegPhoto" (which also appears to be base64 encoded) to UIImage, with the end result being that I display the jpegPhoto as the user's image when they login. More Info: -(NSDictionary *)doQuery:(NSString *)query:(NSArray *)attrsToReturn { ... while(attribute){ if ((vals = ldap_get_values_len(ld, entry, attribute))){ for(int i = 0; vals[i]; i++){ //Uncomment if you want to see all the values. //NSLog(#"%s: %s", attribute, vals[i]->bv_val); if ([resultSet objectForKey:[NSString stringWithFormat:#"%s",attribute]] == nil){ [resultSet setObject:[NSArray arrayWithObject:[NSString stringWithFormat:#"%s",vals[i]->bv_val]] forKey:[NSString stringWithFormat:#"%s",attribute]]; }else{ NSMutableArray *array = [[resultSet objectForKey:[NSString stringWithFormat:#"%s",attribute]] mutableCopy]; [array addObject:[NSString stringWithFormat:#"%s",vals[i]->bv_val]]; [resultSet setObject:array forKey:[NSString stringWithFormat:#"%s",attribute]]; } } ldap_value_free_len(vals); }; ldap_memfree(attribute); attribute = ldap_next_attribute(ld, entry, ber); }; ... } -(UIIMage *)getPhoto{ NSString *query = [NSString stringWithFormat:#"(uid=%#)",self.bindUsername]; NSArray *attrsToReturn = [NSArray arrayWithObjects:#"cn",#"jpegPhoto", nil]; NSDictionary *rs = [self doQuery:query:attrsToReturn]; NSString *photoString = [[rs objectForKey:#"jpegPhoto"] objectAtIndex:0]; NSLog(#"The photoString is: %i %#",[photoString length],#"characters long"); //returns 4 NSData *photoData = [NSData dataWithBase64EncodedString:photoString]; UIImage *userPhoto = [UIImage imageWithData:photoData]; return userPhoto; } - (void)viewDidLoad { [super viewDidLoad]; self.studentNameLabel.text = [NSString stringWithFormat:#"Hi %#!",[self.ldap getFullName]]; self.studentPhotoImage.image = [self.ldap getPhoto]; [self checkForProctor]; }
Try this code NSData *dataObj = [NSData dataWithBase64EncodedString:beforeStringImage]; UIImage *beforeImage = [UIImage imageWithData:dataObj]; There are many similar questions in Stackoverflow.. Please refer the following links UIImage to base64 String Encoding UIImage from bytes held in NSString
(Since there has been no working code posted for getting the image data from LDAP, I wanted to add this answer for the benefit of future visitors.) The missing piece was reading the binary data into an NSData object rather than an NSString when you have binary data that might contain NULL (zero) values within it, such as images or GUIDs. value = [NSData dataWithBytes:vals[0]->bv_val length:vals[0]->bv_len]; + (NSArray *)searchWithBaseDN:(const char *)baseDN andFilter:(const char *)filter andScope:(int)scope { ... while(entry) { // create a dictionary to hold attributes NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init]; attribute = ldap_first_attribute(ld, entry, &ber); while(attribute) { if ((vals = ldap_get_values_len(ld, entry, attribute))) { if (ldap_count_values_len(vals) > 1) { NSMutableArray *values = [[NSMutableArray alloc] init]; for(int i = 0; vals[i]; i++) { [values addObject:[NSString stringWithUTF8String:vals[i]->bv_val]]; } [dictionary setObject:values forKey:[NSString stringWithUTF8String:attribute]]; } else { NSObject *value = nil; if (strcmp(attribute, "thumbnailPhoto") == 0 || strcmp(attribute, "objectGUID") == 0) { value = [NSData dataWithBytes:vals[0]->bv_val length:vals[0]->bv_len]; } else { value = [NSString stringWithFormat:#"%s", vals[0]->bv_val]; } [dictionary setObject:value forKey:[NSString stringWithUTF8String:attribute]]; } ldap_value_free_len(vals); }; ldap_memfree(attribute); attribute = ldap_next_attribute(ld, entry, ber); }; ... }