1

我在 ios 插件中有这个本机代码:

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;

}

然后我在 Unity 端有这段代码(在 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;
    }

我在调试控制台时收到此错误(之前的打印输出):

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 

关键是:我想将本地代码中处理后的图像的字节[]传递给c#

4

0 回答 0