将图像转换为字节数组

时间:2011-02-18 20:32:47

标签: iphone uiimageview bytearray

我希望转换从用户捕获的签名。

我有以下

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

我从一个类似的问题中看到,我的问题是我无法在任何地方使用byteData,它会发回Bad_access错误。我可以将它正确转换为字节数组吗?如果我输出imageData到控制台我得

<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..

2 个答案:

答案 0 :(得分:0)

要将数据转换为字符串使用:

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

如果您想通过HTTP发送,请使用第二个。并确保它是零终止的:

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

答案 1 :(得分:0)

解决了它。问题是我用格式编码它。我需要做一个base64编码。以下网站是base64编码器的答案是http://www.cocoadev.com/index.pl?BaseSixtyFour