Search code examples
.netxamarin.iosuiimageobsoletenserror

MonoTouch: UIImage.asJPEG needs a NSError object


To save a UIImage to a PNG or JPEG locally you call the asPNG().Save(...) function.

The asPNG().Save() function requires an out NSError

The problem is that you can no longer just create a blank NSError to pass in like this (Obsolete)

NSError err = new NSError();  //Obsolete

So, to use the Save() function in MonoTouch, how do we create an NSError() object now?


Solution

  • In .NET you do not have to initialize any out parameter (in contrast to ref parameters) since it's the called method job to do so.

    E.g.

    NSError err; // unitialized
    UIImage img = ...;
    img.AsPNG ().Save (url, true, our err);
    if (err != null && err.Code != 0) {
        // error handling
    }