x


Returning a byte array from ObjC to C# script on ios

I'm trying to create a plugin that lets you grab images from the ALAssetLibrary in the photo album. I have the bytes for a jpeg, and I just need to return them back to my C# script.

Objective C

    UIImage *image = [UIImage imageWithCGImage: [asset thumbnail]];
    NSData* pictureData =  UIImageJPEGRepresentation (image, 1.0);    
    //failed attempt 1 return (const char*)[pictureData bytes];
    //failed attempt 2 return (unsigned char*)[pictureData bytes];
    return (Byte*)[pictureData bytes];

C# code:

DllImport ("__Internal")]
private static extern byte[] _getjpeg(string unique_id);

void applytexture() {
      System.Byte[] thebytes = _getjpeg("pleh");
      var texture = new Texture2D(1,1);
      Debug.Log("Bytes length is "+thebytes.Length);
      texture.LoadImage(thebytes);
      renderer.material.mainTexture = texture;
}

The Objective C code seems to run ok. But the length of the bytes I get back in C# is 16777217. No idea what it is, but its not the 12k I saw in the XCode debugger.

Thanks.

more ▼

asked Jun 19 '11 at 11:29 PM

aicos gravatar image

aicos
71 5 5 10

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Well, it's a problem to get unmanaged memory into managed code. I don't know ObjC (only C++) so the i'm not familiar with the syntax of ObjC. It's easier to do it the other way round. Pass a managed array to ObjC and fill the array. You need to set the array size either big enough, or you have to implement a query function to get the required size.

// C++
int __declspec(dllexport) GetImageData(const char **data, int size)
{
    // access the data array and copy the data. Make sure you don't exceed the size limit.
    // return the copied byte count
}

// C#
[DllImport ("myLib")]
private static extern int GetImageData(byte[] data, int size);

byte[] image_Data= new byte[MAX_SIZE];
int bytes_Copied = GetImageData(image_Data,image_Data.Length);

hope that helps a bit ;) Maybe there's a ObjC expert around

more ▼

answered Jun 20 '11 at 05:08 AM

Bunny83 gravatar image

Bunny83
45.5k 11 49 207

Thanks, will try that. I'm actually doing it in ObjC/C++, so what you've provided there is very close to how I would do it. I'm just a bit uncertain how I would work with the "data" variable you defined in the GetImageData() function. You defnined "data" with consts. So do I need to memory copy the image data into it, or can I do something like:

data = (const **char)[pictureData bytes];

I also wonder if should not be an unsigned *char in both the function parameter and in the above line?

Jun 21 '11 at 12:02 PM aicos
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4172
x1967
x40

asked: Jun 19 '11 at 11:29 PM

Seen: 4656 times

Last Updated: Jun 21 '11 at 02:13 PM