Bug 14278 - Xamarin.iOS bug while saving downscaled photo image files as JPG
Summary: Xamarin.iOS bug while saving downscaled photo image files as JPG
Alias: None
Product: iOS
Classification: Xamarin
Component: General ()
Version: 6.4.0
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2013-08-27 12:15 UTC by Prashant Cholachagudda
Modified: 2013-08-27 15:20 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Test case (65.25 KB, application/zip)
2013-08-27 12:15 UTC, Prashant Cholachagudda

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and 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 Links.

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.

Related Links:

Description Prashant Cholachagudda 2013-08-27 12:15:51 UTC
Created attachment 4725 [details]
Test case

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
Comment 2 Sebastien Pouliot 2013-08-27 15:20:57 UTC
Note: It's not a bug when an API returns a documented value (null).

`UIImage.AsJPEG` is a simple wrapper around `UIImageJPEGRepresentation` [1]. 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` [2].

<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.

[1] https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIKitFunctionReference/Reference/reference.html#//apple_ref/c/func/UIImageJPEGRepresentation

[2] https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImage_Class/Reference/Reference.html#//apple_ref/occ/instp/UIImage/CGImage