Stream to NSData and MOE pointer magic

I am struggling to convert Java data to “native” iOS pointers.
I have an InputStream containing a Jpeg image. I like to load this stream into an UIImage.

I did this before with RoboVM using NSData. How do I create the right “pointer” to my byte array so NSData.dataWithBytesLength() accepts it.

In general I struggle to with all the pointer magic in MOE for example using Apples Security library. Is there a document which explains how to create/handle the pointers so it can be used in Apples native libraries?

// convert InputStream to byte[] to use with NSData
ByteArrayOutputStream arrayStream = new ByteArrayOutputStream();
int nRead;
byte[] buf = new byte[ 16384 ];
while( ( nRead = buf, 0, buf.length ) )!=-1 )
	arrayStream.write( buf, 0, nRead );

byte[] imageData = arrayStream.toByteArray(); "Stream > byte array" );

BytePtr ptr = PtrFactory.newByteArray( imageData ); "byte array > MOE pointer" );

NSData data = NSData.dataWithBytesLength( ptr, imageData.length ); "MOE pointer > NSData "+data.length() ); // >> FAILS! length is 0

stream.close(); "Streams closed" );

UIImage image = UIImage.imageWithData( data ); "Image created: "+image );

Now, once we hopefully have the UIImage loaded if should be the content of an CALayer via a CGImage. In RoboVM this worked, but in MultiOS the image doesn’t show up in the layer.

	CALayer layer = CALayer.alloc().init();
	layer.setContents( image.CGImage() ); // << Does ot work
//	layer.setBackgroundColor( UIColor.redColor().CGColor() );
	layer.setContentsGravity( "ResizeAspectFill" );
	layer.setMasksToBounds( true );
	layer.setFrame( view().bounds() );
	view().layer().insertSublayerAtIndex( layer, 0 )

Stackoverflow gives the answer, but how is this translated into MOE? Any advice?

Dear gradle42,

you need to change the line:

layer.setContents( image.CGImage() ); // << Does ot work


layer.setContents(ObjCRuntime.cast(image.CGImage(), ObjCObject.class));

This is a bit like the ObjC equivalent:

layer.contents = (id) /* <- this */ image.CGImage;

Thanks! That did indeed work.