- Home /
This post has been wikified, any user with enough reputation can edit it.
passing byte[] from native ios (plugin) to c# Unity?
I have this native code in ios plugin:
int _mergeImage(const unsigned char* imageBackground,
int backgroundArraySize, int backgroundWidth, int backgroundHeight,
const unsigned char* imageForeground, int foregroundArraySize,
int foregroundTopLeftX, int foregroundTopLeftY,
int foregroundWidth, int foregroundHeight,
void* dataPtr){
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, imageBackground, backgroundArraySize, NULL);
CGImageRef backImageRef = CGImageCreateWithPNGDataProvider(provider, NULL, false, kCGRenderingIntentDefault);
CGDataProviderRef foregroundProvider = CGDataProviderCreateWithData(NULL, imageForeground, foregroundArraySize, NULL);
CGImageRef foreImageRef = CGImageCreateWithPNGDataProvider(foregroundProvider, NULL, false, kCGRenderingIntentDefault);
UIImage* uiImageBack = [UIImage imageWithCGImage:backImageRef];
UIImage* uiImageFore = [UIImage imageWithCGImage:foreImageRef];
UIGraphicsBeginImageContext(CGSizeMake(backgroundWidth, backgroundHeight));
[uiImageBack drawInRect:CGRectMake(0, 0, backgroundWidth, backgroundHeight)];
[uiImageFore drawInRect:CGRectMake(foregroundTopLeftX, foregroundTopLeftY, foregroundWidth, foregroundHeight)];
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData* data = UIImagePNGRepresentation(result);
NSLog(@"Background array size:%d and data.length:%d", backgroundArraySize, data.length);
dataPtr = data.bytes;
NSLog(@"Done copying...Background array size:%d and data.length:%d", backgroundArraySize, data.length);
return data.length;
}
Then I have this code in Unity side(in C#):
[DllImport("__Internal")]
private static extern int _mergeImage (byte[] imageBackground, int backgroundArraySize,
int backgroundWidth, int backgroundHeight,
byte[] imageForeground, int foregroundArraySize,
int foregroundTopLeftX, int foregroundTopLeftY,
int foregroundWidth, int foregroundHeight, out IntPtr buff);
private static byte[] MergeImage (byte[] imageBackground, int backgroundArraySize,
int backgroundWidth, int backgroundHeight, byte[] imageForeground, int foregroundArraySize,
int foregroundTopLeftX, int foregroundTopLeftY, int foregroundWidth, int foregroundHeight)
{
Debug.Log ("Starting....");
IntPtr unmanagedPtr = IntPtr.Zero;
Debug.Log ("Calling");
int length = _mergeImage (imageBackground, backgroundArraySize, backgroundWidth, backgroundHeight, imageForeground,
foregroundArraySize, foregroundTopLeftX, foregroundTopLeftY, foregroundWidth, foregroundHeight, out unmanagedPtr);
Debug.Log ("Done with length :" + length);
byte[] result = new byte[length];
Debug.Log ("Copying.....");
Marshal.Copy (unmanagedPtr, result, 0, length);
Debug.Log ("Want to free it");
Marshal.FreeHGlobal (unmanagedPtr);
Debug.Log ("Done.....");
return result;
}
I get this error(with previous print out) when i debug console:
Copying.....
ArgumentNullException: Argument cannot be null.
Parameter name: src
at System.Runtime.InteropServices.Marshal.Copy (IntPtr source, System.Byte[] destination, Int32 startIndex, Int32 length) [0x00000] in <filename unknown>:0
at ImageUtilIOS.MergeImage (System.Byte[] imageBackground, Int32 backgroundArraySize, Int32 backgroundWidth, Int32 backgroundHeight, System.Byte[] imageForeground, Int32 foregroundArraySize, Int32 foregroundTopLeftX, Int32 foregroundTopLeftY, Int32 foregroundWidth, Int32 foregroundHeight) [0x00000] in <filename unknown>:0
at ImageUtilIOS.MergeImage (System.Byte[] imageBackgroundPNG, Vector2 backgroundDimension, System.Byte[] imageForegroundPNG, Vector4 foregroundBound) [0x00000] in <filename unknown>:0
The key point is: I want to passing byte[] of the processed image in native code to c#
Comment
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
introducing "safe" DLL causes internal compiler error 0 Answers
Objective-C and C code in unity plugin 1 Answer
Access app_icon from an Android Plugin 2 Answers
Android GCM 1 Answer