ObjC/C++ 中字节数组封送问题IOS 中的 C#

发布于 2024-11-17 03:45:25 字数 2479 浏览 2 评论 0原文

** 这仍然没有解决 **

我正在尝试从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘿看小鸭子会跑 2024-11-24 03:45:25

请记住,JPGRepresentation 可能与您输入的内容不完全相同,因此长度可能会有所不同。

如果

for(int i;i < picturesize;i++) {
    // *** Not sure I'm doing this correctly ***
    *returnbytes[i] = bytes[i];            
}

您忘记初始化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

for(int i;i < picturesize;i++) {
    // *** Not sure I'm doing this correctly ***
    *returnbytes[i] = bytes[i];            
}

you forget to initialize i, so it might start with a random value which is bigger than picturesize, so the loop won't run at all.

情未る 2024-11-24 03:45:25

您需要 unsigned char*,而不是 **。您正在传递一个已分配的指针。 ** 用于当您传递一个指向变量的指针时,该变量本身就是一个指向数据的指针:即,当被调用者将分配内存并且调用者想要了解它时。

只需传入 unsigned char* ,然后使用

returnbytes[i] = bytes[i];

或者,在 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

returnbytes[i] = bytes[i];

Alternatively, allocate in the calee and use an out, not a ref.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文