** 这个还没解决**
我正在尝试从 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 等。
** 更新**
更正了循环初始值设定项,现在它在 *returnbytes[i] = bytes[i]; 上给出了一个 EXC_BAD_ACCESS 信号;行。
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;
}
谢谢
最佳答案
请记住,JPGRepresentation
可能与您输入的不完全相同,因此长度可能不同。
在
for(int i;i < picturesize;i++) {
// *** Not sure I'm doing this correctly ***
*returnbytes[i] = bytes[i];
}
你忘记初始化 i
,所以它可能以一个大于 picturesize
的随机值开始,所以循环根本不会运行。
关于c++ - 将 ObjC/C++ 中的字节数组编码到 IOS 中的 C# 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6485654/