Bug 9240 - After scaling UIImage.CGImage returns a CGImage with twice the width and height of the scaled UIImage
Summary: After scaling UIImage.CGImage returns a CGImage with twice the width and heig...
Status: RESOLVED FEATURE
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: 6.0.x
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2013-01-03 13:57 UTC by Ben Holmes
Modified: 2013-01-14 09:27 UTC (History)
2 users (show)

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

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:
Status:
RESOLVED FEATURE

Description Ben Holmes 2013-01-03 13:57:34 UTC
Scaling a UIImage with UIImage.Scale returns a UIImage with the correct size but when getting the CGImage reference from the object its size 2x larger in both width and height.

Example:

UIImage tempImg = UIImage.FromFile("textImg.jpg");

tempImg = tempImg.Scale (new SizeF (100f, 100f));

Debug.Print(tempImg.CGImage.Width.ToString());
Debug.Print(tempImg.CGImage.Height.ToString());

This example returns the following:

200
200

The same is true when this is done in-place like so:

UIImage tempImg = UIImage.FromFile("textImg.jpg");

Debug.Print(tempImg.Scale (new SizeF (100f, 100f).CGImage.Width.ToString());
Debug.Print(tempImg.Scale (new SizeF (100f, 100f).CGImage.Height.ToString());
Comment 1 Sebastien Pouliot 2013-01-03 21:16:57 UTC
I made a test using an existing (32x32) image and I cannot reproduce this.

			string file = Path.Combine (NSBundle.MainBundle.ResourcePath, "basn3p08.png");
			var original = UIImage.FromFile (file);
			Assert.That (original.CGImage.Width, Is.EqualTo (32f), "Width-a");
			Assert.That (original.CGImage.Height, Is.EqualTo (32f), "Height-a");
			SizeF half = new SizeF (16f, 16f);
			original = original.Scale (half);
			Assert.That (original.CGImage.Width, Is.EqualTo (16), "Width-b");
			Assert.That (original.CGImage.Height, Is.EqualTo (16), "Height-b");

Note that your 2nd code snippet won't compile (the parenthesis don't match).

> Debug.Print(tempImg.Scale (new SizeF (100f, 100f).CGImage.Width.ToString());
> Debug.Print(tempImg.Scale (new SizeF (100f, 100f).CGImage.Height.ToString());

So maybe your project was not rebuilded before being executed ? and you're seeing old values being printed (e.g. from a previous binary) ?

If not then please attach a small test case, including the textImg.jpg, file that you used so we can investigate this further.
Comment 2 Sebastien Pouliot 2013-01-14 09:27:05 UTC
This seems to happen only on devices and AFAICT it's an iOS optimization. IOW the backing image is not modified until (and if) it's needed.

Such optimization can save memory (and CPU time) on several scenarios, e.g.:
* several sizing/rotating operations can be merged into one;
* hardware acceleration (GPU) might not require the backing image to be resized in memory;

However that should not affect your code, i.e. if you scale an `UIImage` then you should look at _it's_ Size property (not the backing image size).