Bug 8260 - Save huge photos to disk
Summary: Save huge photos to disk
Alias: None
Product: iOS
Classification: Xamarin
Component: General ()
Version: 6.0.x
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2012-11-07 09:20 UTC by Gabriel Dana
Modified: 2012-11-13 07:05 UTC (History)
2 users (show)

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:

Description Gabriel Dana 2012-11-07 09:20:54 UTC
I'm trying to save a 10Mb photo to disk and crashes on device.

Photo = http://upload.wikimedia.org/wikipedia/commons/f/ff/Pizigani_1367_Chart_10MB.jpg

NSUrl idest = NSUrl.FromFilename (filePath);
using (CGImageDestination d = CGImageDestination.FromUrl(idest,"public.png",1))
   d.AddImage (Singleton.Instance.uploadedImage.CGImage,null);
Comment 1 Sebastien Pouliot 2012-11-09 18:07:43 UTC
FWIW this code works on my iPad 3:

	using (NSUrl url = new NSUrl ("http://upload.wikimedia.org/wikipedia/commons/f/ff/Pizigani_1367_Chart_10MB.jpg"))
	using (NSData data = NSData.FromUrl (url)) 
	using (UIImage img = UIImage.LoadFromData (data))
	using (NSUrl idest = NSUrl.FromFilename (Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments), "test.png"))) {
		using (CGImageDestination d = CGImageDestination.FromUrl(idest,"public.png",1)) {
			d.AddImage (img.CGImage, null);
			d.Close ();

I'm not sure what your crash was (no attachment) but I guess iOS ran out of memory and killed your app (either by crashing or a "proper" kill after warnings).

Now that's a HUGE, 10315 x 7049 pixels, bitmap which must be decompressed in memory before being (recompressed and) saved as a PNG. Even showing this in Safari (OSX) takes quite some time.

I'm afraid your device(s) might not have enough memory to do so (the simulator does not have similar memory restrictions). While the iPad3 has 1 Gb most devices have 512mb or even 256mb of RAM (and no virtual memory).

My suggestion is that you should query the image metadata before trying to decompress it. If it's too big for your device(s) then warn the user and avoid the decompression.
Comment 2 Gabriel Dana 2012-11-12 09:06:32 UTC
Thank you for the suggestion. So there is no solution to save this huge image...

I want to access asset dimensions with: "asset.DefaultRepresentation.Dimensions.Height"
works ok on the simulator but crashes on the device, the Dimensions is null

Is there any other way to get image width or height ?
Comment 3 Sebastien Pouliot 2012-11-12 09:12:04 UTC
You can do so with CGImageSource (ImageIO API), see: http://stackoverflow.com/q/10753108/220643
Comment 4 Gabriel Dana 2012-11-12 09:30:51 UTC
						using (var imageSource = CGImageSource.FromUrl (rep.Url)) {
							CGImageOptions options = new CGImageOptions () { ShouldCache = false };
							using (var props = imageSource.CopyProperties (options)) {
								GeneralUtil.log("props = " + props);

props is empty, maybe the image doesn't contain this metadata. It's a downloaded JPG

also in the last comment from http://stackoverflow.com/questions/10753108/how-to-get-the-image-size-of-an-image-file-in-monotouch-without-loading-it says:

"Looks like this is a bug in Mono Touch. I tested the code in XCode with the same image in Mono Touch and it worked in XCode.

In Mono Touch after calling the imageSource.CopyProperties(imageProperties,0) imageProperties.Keys Count is 0

So you should create a bug report at bugzilla.xamarin.com"
Comment 5 Sebastien Pouliot 2012-11-12 10:13:05 UTC
Well there's a typo (I'll fix it in my SO answer) and it was part of the answer you copy-pasted, i.e.

don't do: imageSource.CopyProperties (options)
do: imageSourceCopyProperties(imageProperties,0)

IOW specify the image index (otherwise you'll only get the filesize).

Anyway CopyProperties is not obsolete was we provide an easier way to get the "basic" properties (the full list is ay too long) using GetProperties. E.g.

	using (NSUrl url = new NSUrl ("http://upload.wikimedia.org/wikipedia/commons/f/ff/Pizigani_1367_Chart_10MB.jpg")) {
		var p = src.GetProperties (0, null);
		Console.WriteLine ("{0}x{1}", p.PixelWidth, p.PixelHeight);

will print:

Comment 6 Gabriel Dana 2012-11-13 07:05:30 UTC
Thank you.

We fixed the problem with something similar with this: