Bug 16269 - UIImage.AsPNG throws InvalidCastException
Summary: UIImage.AsPNG throws InvalidCastException
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: 7.0.4.x
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Sebastien Pouliot
Depends on:
Reported: 2013-11-15 12:25 UTC by Adam Kemp
Modified: 2014-04-18 10:35 UTC (History)
6 users (show)

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

Xamarin test project (86.70 KB, application/zip)
2013-11-15 12:25 UTC, Adam Kemp
Xcode project (does not reproduce the issue) (191.12 KB, application/zip)
2013-11-15 12:26 UTC, Adam Kemp
picture (237.86 KB, image/jpeg)
2013-11-15 13:31 UTC, Sebastien Pouliot

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 Adam Kemp 2013-11-15 12:25:50 UTC
Created attachment 5442 [details]
Xamarin test project

When I try to use UIImage.AsPNG on a device it throws an InvalidCastException. The equivalent ObjectiveC code does not crash.

I tested on an iPad running iOS 7.

To reproduce the issue run the app in the attached project and then press the share button in the top right of the app. If the popup appears then the issue did not reproduce. On my device we get an exception before the popup appears (when we try to save the image to share).
Comment 1 Adam Kemp 2013-11-15 12:26:39 UTC
Created attachment 5443 [details]
Xcode project (does not reproduce the issue)

This is the equivalent code in ObjectiveC. It does not crash.
Comment 2 Sebastien Pouliot 2013-11-15 13:24:00 UTC
Works on an iPod w/iOS 7.0.3 (once I made the app universal). Need to do the provisioning-dance to test on iPad...
Comment 3 Sebastien Pouliot 2013-11-15 13:31:33 UTC
Created attachment 5446 [details]

Works on iPad (3rd gen) running iOS 7.0.2.

Can you tell us what software version you're using ?

The easiest way to get exact version information is to use the "Xamarin Studio" menu, "About Xamarin Studio" item, "Show Details" button and copy/paste the version informations (you can use the "Copy Information" button).
Comment 4 Adam Kemp 2013-11-15 14:10:51 UTC
I was running on an iPad Mini (the original) with iOS 7.0.3. Here is the Xamarin software info:

=== Xamarin Studio ===

Version 4.0.12 (build 3)
Installation UUID: 5262c8cb-ead0-41f7-926c-685725d7430e
	Mono 3.2.4 ((no/294f999)
	GTK 2.24.20
	GTK# (
	Package version: 302040000

=== Apple Developer Tools ===

Xcode 5.0.2 (3335.32)
Build 5A3005

=== Xamarin.Mac ===

Xamarin.Mac: Not Installed

=== Xamarin.iOS ===

Version: (Trial Edition)
Hash: 23a0827
Build date: 2013-11-11 16:04:00-0500

=== Xamarin.Android ===

Version: 4.10.1 (Starter Edition)
Android SDK: /Users/adam/Library/Developer/Xamarin/android-sdk-mac_x86
	Supported Android versions:
		2.1   (API level 7)
		2.2   (API level 8)
		2.3   (API level 10)
		3.1   (API level 12)
		4.0   (API level 14)
		4.0.3 (API level 15)
Java SDK: /usr
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)

=== Build Information ===

Release ID: 400120003
Git revision: 593d7acb1cb78ceeeb482d5133cf1fe514467e39
Build date: 2013-08-07 20:30:53+0000
Xamarin addins: 25a0858b281923e666b09259ad4746b774e0a873

=== Operating System ===

Mac OS X 10.9.0
Darwin varlamov.amer.corp.natinst.com 13.0.2 Darwin Kernel Version 13.0.2
    Sun Sep 29 19:38:57 PDT 2013
    root:xnu-2422.75.4~1/RELEASE_X86_64 x86_64
Comment 5 Sebastien Pouliot 2013-11-15 14:34:18 UTC
Gouri, do you have an iPad mini (non-retina) with iOS7 to try the attached (C#) sample ?
Comment 6 GouriKumari 2013-11-15 16:18:22 UTC
I tested the sample on iPadMin (A1432) and iPad2 . But couldn't reproduce the issue. 

Supplemental Info:
ipadMini Logs:
ConsoleLogs: https://gist.github.com/GouriKumari/decfc20a8c13a6e9ce5f
Application Output:  https://gist.github.com/GouriKumari/6fc3379be052255c570a

Test Env:
Xamarin Studio
Version 4.2.1 (build 1)
Installation UUID: 6b08c64b-dbf4-4817-b167-d94ad0b26ad5
	Mono 3.2.4 ((no/294f999)
	GTK+ 2.24.20 theme: Raleigh
	GTK# (
	Package version: 302040000

Version: 4.8.0
Android SDK: Not found

Apple Developer Tools
Xcode 5.0 (3332.25)
Build 5A1413

Version: (Business Edition)
Hash: 23a0827
Build date: 2013-11-11 16:04:00-0500

Xamarin.Mac: Not Installed

Build Information
Release ID: 402010001
Git revision: 844a84fe0aa0cb5f986d4e3c4807a51487d07845
Build date: 2013-11-13 22:12:16+0000
Xamarin addins: 97e44e4863da6c479427794457637e75b3d22600

Operating System
Mac OS X 10.8.5
Darwin dynamic-099.bos.xamarin.com 12.5.0 Darwin Kernel Version 12.5.0
    Sun Jun 23 20:53:53 PDT 2013
    root:xnu-2050.48.10~1/RELEASE_X86_64 x86_64
Comment 7 GouriKumari 2013-11-15 16:19:28 UTC
Updates to the above comment:

Test Env:

Device info:
iPadMini: 7.0.3
iPad2 : 7.0.2
Comment 8 Sebastien Pouliot 2013-11-15 16:29:04 UTC
@Gouri Thanks!

@Adam can you add "-v -v -v -v" to your "Additional mtouch arguments", then rebuild the solution and attach the build log ?

I'll compare it to mine to see if anything stands out.
Comment 9 Adam Kemp 2013-11-15 17:18:03 UTC
Unfortunately I can no longer reproduce this either. I could reproduce it reliably (across clean builds, even) all day yesterday and this morning, but not now. That could be because I spent all afternoon alternately installing Xamarin.iOS 7.0.2 and 7.0.4 to investigate another issue we're having. Maybe this was a bad install that I inadvertently fixed? I have no idea.
Comment 10 Sebastien Pouliot 2013-11-16 11:26:16 UTC
Ok, if you ever encounter this issue again can you try creating a backup of your project directory ? i.e. including all temporary files.

Like I said on the forum post this happened a few times and it always "fixed" itself without us being able to reproduce it. Hopefully the additional details we get, each time, will allow us to spot something common across them. Thanks for your help!
Comment 11 Scott 2014-04-18 10:35:26 UTC
I am seeing this same issue. Happened consistently while using device (iPhone 4S 7.1 11D167) until I deployed to the simulator, then deployment thereafter to the device worked.

Unhandled managed exception: Cannot cast from source type to destination type. (System.InvalidCastException)
at MonoTouch.UIKit.UIImage.AsPNG () [0x00006] in /Developer/MonoTouch/Source/monotouch/src/UIKit/.pmcs-compat.UIImage.cs:44

=== Xamarin Studio ===

Version 4.3.4 (build 0)
Installation UUID: 6441b89d-c4e1-486c-9d9e-67017b363a32
	Mono 3.4.0 ((no/d4511ef)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 304000185

=== Apple Developer Tools ===

Xcode 5.1.1 (5085)
Build 5B1008

=== Xamarin.Mac ===

Xamarin.Mac: Not Installed

=== Xamarin.Android ===

Not Installed

=== Xamarin.iOS ===

Version: (Indie Edition)
Hash: 86b1dbd
Build date: 2014-04-11 23:42:07-0400

=== Build Information ===

Release ID: 403040000
Git revision: e2e118a56ebf05c9f15eafbf53379bdebded727c
Build date: 2014-03-12 15:39:07-04
Xamarin addins: 9f2eb9aed862e28ca23d2d2f34e22952087e6e4c

=== Operating System ===

Mac OS X 10.9.2
Darwin devel.local 13.1.0 Darwin Kernel Version 13.1.0
    Thu Jan 16 19:40:37 PST 2014
    root:xnu-2422.90.20~2/RELEASE_X86_64 x86_64