Home Features Docs Blog Support GitHub

Creating JPEG from image bits fails when calling UIKit.UIImageJPEGRepresentation

(Guilherme Campos Hazan) #1


I’m trying to create a jpeg from image bits. I found this tutorial and adapted it to MOE.

My code follows:

  int[] pixels = src.getPixels(); // src is a class that holds pixels and width/height
  int width = src.getWidth(), height = src.getHeight();
  int bitmapInfo = CGBitmapInfo.ByteOrder32Big | CGImageAlphaInfo.PremultipliedLast;
  //Save image
  CGDataProviderRef dataProviderRef = CoreGraphics.CGDataProviderCreateWithData(null, PtrFactory.newIntArray(pixels), pixels.length, null);
  CGImageRef imageRef = CoreGraphics.CGImageCreate(width, height, 8, 32, width * 4, CoreGraphics.CGColorSpaceCreateDeviceRGB(), bitmapInfo,
     dataProviderRef, null, false, CGColorRenderingIntent.Default);
  UIImage newImage = UIImage.alloc().initWithCGImageScaleOrientation(imageRef, 1.0, UIImageOrientation.Up);
  NSData data = UIKit.UIImageJPEGRepresentation(newImage, 0.85f); // ***RETURNS NULL***
  int len = (int)data.length();
  byte[] ret = new byte[len];
  ConstBytePtr p = data.bytes().getBytePtr();
  out.write(ret); // write to OutputStream

Debugging in my iPad shows that the UIImageJPEGRepresentation method call fails returning null. The documentation says:

RETURNS: A data object containing the JPEG data, or nil if there was a problem generating the data. This function may return nil if the image has no data or if the underlying CGImageRef contains data in an unsupported bitmap format.

I run this code from a separate thread, so i also tried to put the UIKit part to run inside the dispatch thread, but it still returns null.

Thanks in advance


(Guilherme Campos Hazan) #2

Found the reason: the input buffer must be byte[], not int[] !