c++ - 将 ObjC/C++ 中的字节数组编码到 IOS 中的 C# 时出现问题

标签 c++ arrays ios xamarin.ios marshalling

** 这个还没解决**

我正在尝试从 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/

相关文章:

c++ - 在 cygwin 上找不到 tchar.h

python - numpy数组被切片两次

javascript - 如何循环遍历数组并创建带有类别的术语表列表

ios - RestKit 和 Swift 错误响应描述符

c++ - 在 C++11 中从 C++17 重新实现 std::map::try_emplace()?

c# - 实现这个的最佳数据结构是什么? [C# 中的嵌套属性]

ios - 在 Objective C 中检查一个数组是否包含另一个数组的对象

ios - SWRevealViewController 委托(delegate) nil

iphone - 在iOS中滚动立方体效果的tableview单元格?

C++ 泛型链表