Notice (2018-05-24): bugzilla.xamarin.com is now in
Please join us on
Visual Studio Developer Community and in the
Mono organizations on
GitHub to continue tracking issues. Bugzilla will remain
available for reference in read-only mode. We will continue to work
on open Bugzilla bugs, copy them to the new locations
as needed for follow-up, and add the new items under Related
Our sincere thanks to everyone who has contributed on this bug
tracker over the years. Thanks also for your understanding as we
make these adjustments and improvements for the future.
Please create a new report on
Developer Community or GitHub with
your current version information, steps to reproduce, and relevant error
messages or log files if you are hitting an issue that looks similar to
this resolved bug and you do not yet see a matching new report.
Created attachment 4725 [details]
Xamarin.iOS bug while saving downscaled photo image files as JPG.
UIImage.AsJPEG method returns null, where image is output of CILanczosScaleTransform.OutputImage of CIImage
Note: It's not a bug when an API returns a documented value (null).
`UIImage.AsJPEG` is a simple wrapper around `UIImageJPEGRepresentation` . It can return null, as documented, when:
<quote>nil if there was a problem generating the data.</quote>
<quote>This function may return nil if the image has no data</quote>
<quote>or if the underlying CGImageRef contains data in an unsupported bitmap format.<quote>
The sample shows that `img.CGImage.Handle` is `IntPtr.Zero` which falls in the last category.
That's normal (i.e. not a bug) when an `UIImage` is created from a `CIImage`. It's documented as such in `UIImage.CGImage` .
<quote>If the UIImage object was initialized using a CIImage object, the value of the property is NULL.</quote>
So what's needed ?
You need CoreImage to get you a CGImage (that's not something UIKit will do for you, a good thing since it would be iOS specific and would not work on OSX).
E.g. based on the test case (fix the rectangle to match your needs)
CIContext ctx = CIContext.FromOptions (null);
var cgimg = ctx.CreateCGImage (resizedImg, new System.Drawing.RectangleF (0, 0, pixelWidth.Value, pixelHeight.Value));
return UIImage.FromImage(cgimg, 1f, outputOrientation);
^ that will return a non-null NSData with JPEG data.