iOS Image picker Plugin: Return image data from xcode

Hello all,

I do not know how to return an image’s data from xcode

Normally, to get data something like “bool” function

extern “C” {
//Something like this
bool _IsThisOnXcode()
{
//return bool type of data
}
}

So, how do I call the function to get image data from xcode?

extern “C” {
??? _GetImage()
{
/return image data
//PNG data
}
}

Thank you!

take a look at unity game engine - How to send UIImage data to Unity3d via a C# script and iOS plug-in? - Stack Overflow

I could not get the const char* to return to C#

On Xcode:
const char* _GetImage(){
NSString *sString = @“Something”;
const char *cString = [sString UTF8String];
return cString;
}

On C#:
[DllImport (“__Internal”)]
private static extern string _GetImage();

public static string GetImage()
{
if (Application.platform != RuntimePlatform.OSXEditor)
{
return _GetImage();
} else {
return @“Hello”;
}
}

I always get this error: pointer being freed was not allocated

http://www.mono-project.com/Interop_with_Native_Libraries#Strings_as_Return_Values
there is mem ownership transfer, so your code is wrong :wink:

I just followed what they did on this link:

http://stackoverflow.com/questions/9107792/how-to-send-uiimage-data-to-unity3d-via-a-c-sharp-script-and-ios-plug-in

So, what is the best way to do it?

I want to return string from xcode

You must return allocated pointer.

char* MakeStringCopy (const char* string) {
if (string == NULL)
return NULL;

char* res = (char*)malloc(strlen(string) + 1);
strcpy(res, string);
return res;
}

const char* _GetImage(){
NSString *sString = @“Something”;
const char *cString = [sString UTF8String];
return MakeStringCopy(cString);
}

Another way, Return an allocated Color32 array pointer. I did so on my plugin;). I never use UIImagePNGRepresentation.

Asset Store Link
https://www.assetstore.unity3d.com/#/content/9083

Got it to work now!

Thank you

Thank you for the link

I will check it out

Hi.

How do you go about returning a Color32 from Xcode? I’d like to be able to load all image thumbnails from iOS photo roll into Unity to display as Texture 2D in Unity.

Thanks!