0

I am looking to convert a signature that is captured from the user.

I have the following

NSData *imageData = UIImagePNGRepresentation(drawImage.image);
    NSUInteger len = [imageData length];
    byteData = (Byte*)malloc(len);
    memcpy(byteData, [imageData bytes], len);

Which I saw from a similar question, My problem is I can't use byteData anywhere, it shoots back a Bad_access error. E Am I converting it properly to a byte Array? If I output imageData to console i get

<89504e47 0d0a1a0a 0000000d 49484452 00000140 0000016f 08060000 003b6a12 49000020 00494441 547801ed 9d07b464 4599c71d 494a5219 5832c210 84251d19 51118519 755d0c18 402489a0 b206c015 44443d0a a8e82a8a 804a7005 040cc0c2 022eac89 a30c02a3 4bf02c41 24391266 605601c9 9267ffff 377d39df f474bfd7 affb7657 75d7afce f9dead9b eaabfa7d f7febb6e ddf0a62c 58b0e079 24084000 02251278 7e898da6 cd108000 044c0001 e4388000 048a2580 00161b7a 1a0e0108 20801c03 108040b1 0410c062 434fc321 00010490 63000210 28960002 586ce869 38042080 00720c40 0002c512 40008b0d 3d0d8700 0410408e 010840a0 58020860 b1a1a7e1 10800002 c8310001 08144b00 012c36f4 341c0210 40003906 20008162 092080c5 869e8643 00020820 c7000420 502c0104 b0d8d0d3 70084000 01e41880 00048a25 8000161b 7a1a0e01 0820801c 03108040 b10410c0 62434fc3 21000104 90630002 10289600 02586ce8 69380420 8000720c 400002c5 1240008b 0d3d0d87 00041040 8e010840 a0580208 60b1a1a7 e1108000 02c83100 0108144b 00012c36 f4341c02 10400039 06200081 62092080 c5869e86 43000208 20c70004 20502c01 04b0d8d etc..
mike
  • 97
  • 10
  • everything seems to be OK here. Can you post the code where you get the crash? – Max Feb 18 '11 at 21:01
  • i fixed my issue with not being able to use byteData, but how would i convert this byteData to a string? – mike Feb 18 '11 at 21:08

2 Answers2

0

To convert data to string use:

[NSString stringWithCString: encoding:];
[NSString stringWithUTF8String:];

If you want to send it via HTTP use the second one. And make sure it is zero-terminated:

byteData = (Byte*)calloc(len+1, sizeof(Byte));

Max
  • 16,679
  • 4
  • 44
  • 57
  • I am failing at writing the line to convert it to string [NSString stringWithUTF8String:];. I had this before NSString *string = [ [NSString alloc] initWithData:imageData encoding:NSASCIIStringEncoding ]; – mike Feb 18 '11 at 21:20
  • what do you mean you're failing? – Max Feb 18 '11 at 21:25
  • I mean it is not working. When I do the second part I wrote. and use imageData I get PNG as the result, When I try and use use byteData, i get a bad_acess at the conversion. – mike Feb 18 '11 at 21:28
  • that's probably because it's not null-terminated. see my edit to answer – Max Feb 18 '11 at 21:31
  • NSString *string = [[NSString stringWithUTF8String:byteData]; is incorrect as i get a warning of differ in signedness which comes from it wanted to get a string and i send data. Which is making me thinking how it that line gonna work ? – mike Feb 18 '11 at 21:38
  • what is the end purpose? what do you want to do with that data? – Max Feb 18 '11 at 21:42
  • I need to convert it to a string to send to my webservice. side question NSString *string = [[NSString alloc] initWithData:byteData encoding:NSUTF8Encoding]; Its telling me NSUTF8Encoding undeclared – mike Feb 18 '11 at 21:47
  • Not NSUTF8Encoding. Use NSUTF8StringEncoding. If you want to send it to your webservice you don't need to convert it to byte array. Just use NSData object and append the post body with it. – Max Feb 18 '11 at 21:50
  • Well mywebservice expects a string when I send it, but could you show a example of what you mean with appending the post body – mike Feb 18 '11 at 21:53
  • http://stackoverflow.com/questions/125306/how-can-i-upload-a-photo-to-a-server-with-the-iphone – Max Feb 18 '11 at 21:57
  • i think that will be tough, since my webservice expects a string in the soap call. I am gonna continue to work on the trying to solve it this way. I adaded the byteData line u mentioned but whenver i try and do this line NSString *string = [[NSString alloc] initWithData:byteData encoding: NSUTF8StringEncoding]; I get Bad_Access . I cant seem to figure out why? – mike Feb 18 '11 at 22:10
0

Solved it. Problem was i was encoding it with the format. I need to do a base64 Encoding. The following the site was was where the answer for the base64 encoder is http://www.cocoadev.com/index.pl?BaseSixtyFour

mike
  • 97
  • 10