Jecky Jecky - 1 month ago 12
Objective-C Question

Converting Data to string gets nil value

I am working on a project in which I have to store image into sqlite database. but when I try to convert NSData to NSString It returns nil value.

Here is my code.

imageData = [[NSData alloc]initWithBytes:UIImagePNGRepresentation(self.img_userprofile.image).bytes length:UIImagePNGRepresentation(self.img_userprofile.image).length];
NSString *charlieSendString = [[NSString alloc] initWithData:imageData encoding:NSUTF8StringEncoding];
NSString *query = [NSString stringWithFormat:@"insert into Friends values(null, '%@', '%@', '%@')", self.txt_name.text,charlieSendString, self.txt_mobile_no.text ];
[self.dbManager executeQuery:query];

// If the query was successfully executed then pop the view controller.

if (self.dbManager.affectedRows != 0) {
NSLog(@"Query was executed successfully. Affected rows = %d", self.dbManager.affectedRows);
}
else{
NSLog(@"Could not execute the query.");
}


Help me thank you

Answer
imageData = [[NSData alloc]initWithBytes:UIImagePNGRepresentation(self.img_userprofile.image).bytes length:UIImagePNGRepresentation(self.img_userprofile.image).length];
NSString *strEncodeImg = [Base64 encode:imageData];

We have download Base64 library from this link : https://github.com/bborbe/base64-ios/tree/master/Base64