ObjC/C++ 中字节数组封送问题IOS 中的 C#
** 这仍然没有解决 **
我正在尝试从 C# 调用 ObjC/C++ 函数代码。我已尽力遵循不同的示例代码,最新的代码主要来自:
http://msdn.microsoft.com/en-us/library/ms146631(v=VS.80).aspx
这是针对 iPhone/MonoTouch 环境,因此我不确定我是否已经做了我应该做的一切。 ObjC/C++ 函数中的字节似乎没问题,但我返回 C# 的字节数组最终包含 0 0 0 0 0 0 等。
** 更新 **
更正了循环初始值设定项,现在它给出了 EXC_BAD_ACCESS 信号上 *returnbytes[i] = bytes[i];线。
C# 代码:
[DllImport ("__Internal")]
private static extern int _getjpeg(string url,ref IntPtr thebytes);
void somefunction(string image_id) {
int maxsize = 50000;
byte[] thebytes = new byte[maxsize];
IntPtr byteptr = Marshal.AllocHGlobal(maxsize);
int imagesize = _getjpeg(image_id,ref byteptr);
Debug.Log("Getting _picturesize()... "+ image_id);
int picsize = _picturesize();
Marshal.Copy(byteptr,thebytes,0,picsize);
var texture = new Texture2D(1,1);
string bytedebug = "";
for (int i=5000 ; i < 5020 ; i++)
bytedebug+=thebytes[i] + " ";
Debug.Log("Bytes length is "+imagesize);
Debug.Log("Bytes content is "+bytedebug);
}
C++/ObjC 代码:
int _getjpeg(const char* url,unsigned char** returnbytes) {
ALAsset* asset = [_pictures objectForKey:[NSString stringWithUTF8String:url]];
if(asset != NULL)
NSLog(@"_getjpeg() found URL: %@",[NSString stringWithUTF8String: url]);
else {
NSLog(@"_getjpeg() could not find URL: %@",[NSString stringWithUTF8String: url]);
return NULL;
}
UIImage *image = [UIImage imageWithCGImage: [asset thumbnail]];
NSData* pictureData = UIImageJPEGRepresentation (image, 1.0);
picturesize = (int)[pictureData length];
unsigned char* bytes = (unsigned char*)[pictureData bytes];
// This test does not give EXC_BAD_ACCESS
*returnbytes[5] = (unsigned int)3;
// updated below initializer in below for loop according to Eikos suggestion
for(int i=0 ; i < picturesize ; i++) {
// below lines gives EXC_BAD_ACCESS
*returnbytes[i] = bytes[i];
}
NSString* debugstr = [NSString string];
for(int i=5000; i < 5020 ; i++) {
unsigned char byteint = bytes[i];
debugstr = [debugstr stringByAppendingString:[NSString stringWithFormat:@"%i ",byteint]];
}
NSLog(@"bytes %s",[debugstr UTF8String]);
return picturesize;
}
谢谢
** This is still unsolved **
I'm trying to call an ObjC/C++ function code from C#. I've done my best to follow different example code, the latest being mostly from:
http://msdn.microsoft.com/en-us/library/ms146631(v=VS.80).aspx
This is for an iPhone/MonoTouch environment, so I'm not sure I've done everything I should. The bytes appear to be ok in the ObjC/C++ function, but the byte array I get back into C# ends up containing 0 0 0 0 0 0 etc.
** Update **
Corrected for loop initializer, and now its giving a EXC_BAD_ACCESS signal on the *returnbytes[i] = bytes[i]; line.
C# code:
[DllImport ("__Internal")]
private static extern int _getjpeg(string url,ref IntPtr thebytes);
void somefunction(string image_id) {
int maxsize = 50000;
byte[] thebytes = new byte[maxsize];
IntPtr byteptr = Marshal.AllocHGlobal(maxsize);
int imagesize = _getjpeg(image_id,ref byteptr);
Debug.Log("Getting _picturesize()... "+ image_id);
int picsize = _picturesize();
Marshal.Copy(byteptr,thebytes,0,picsize);
var texture = new Texture2D(1,1);
string bytedebug = "";
for (int i=5000 ; i < 5020 ; i++)
bytedebug+=thebytes[i] + " ";
Debug.Log("Bytes length is "+imagesize);
Debug.Log("Bytes content is "+bytedebug);
}
C++/ObjC code:
int _getjpeg(const char* url,unsigned char** returnbytes) {
ALAsset* asset = [_pictures objectForKey:[NSString stringWithUTF8String:url]];
if(asset != NULL)
NSLog(@"_getjpeg() found URL: %@",[NSString stringWithUTF8String: url]);
else {
NSLog(@"_getjpeg() could not find URL: %@",[NSString stringWithUTF8String: url]);
return NULL;
}
UIImage *image = [UIImage imageWithCGImage: [asset thumbnail]];
NSData* pictureData = UIImageJPEGRepresentation (image, 1.0);
picturesize = (int)[pictureData length];
unsigned char* bytes = (unsigned char*)[pictureData bytes];
// This test does not give EXC_BAD_ACCESS
*returnbytes[5] = (unsigned int)3;
// updated below initializer in below for loop according to Eikos suggestion
for(int i=0 ; i < picturesize ; i++) {
// below lines gives EXC_BAD_ACCESS
*returnbytes[i] = bytes[i];
}
NSString* debugstr = [NSString string];
for(int i=5000; i < 5020 ; i++) {
unsigned char byteint = bytes[i];
debugstr = [debugstr stringByAppendingString:[NSString stringWithFormat:@"%i ",byteint]];
}
NSLog(@"bytes %s",[debugstr UTF8String]);
return picturesize;
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请记住,
JPGRepresentation
可能与您输入的内容不完全相同,因此长度可能会有所不同。如果
您忘记初始化
i
,因此它可能会以大于picturesize
的随机值开始,因此循环根本不会运行。Keep in mind that the
JPGRepresentation
is probably not exactly the same as you put into it, so the length may differ.In
you forget to initialize
i
, so it might start with a random value which is bigger thanpicturesize
, so the loop won't run at all.您需要
unsigned char*
,而不是**
。您正在传递一个已分配的指针。**
用于当您传递一个指向变量的指针时,该变量本身就是一个指向数据的指针:即,当被调用者将分配内存并且调用者想要了解它时。只需传入 unsigned char* ,然后使用
或者,在 calee 中分配并使用 out,而不是 ref。
You want
unsigned char*
, not**
. You are passing a pointer in that is already allocated. A**
is for when you are passing in a pointer to variable that is itself a pointer to data: i.e. when the callee will allocate the memory and the caller wants to know about it.Just pass in unsigned char* and then use
Alternatively, allocate in the calee and use an out, not a ref.