Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
2
Question by agp12 · Mar 12, 2013 at 09:52 AM · plugin

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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges