Search code examples
iosxamarin.iosuiimagecgimage

Can CGImage created from file be null


Can image.CGImage ever return null if UIImage was being created from file or from bundle?

UIImage image = UIImage.FromFile(name);
// or
UIImage image = UIImage.FromBundle(name);
// image.CGImage == null?

Solution

  • Thanx to @BytesGuy for help. As he said UIImage.FromFile and UIImage.FromBundle can't return UIImage with null CGImage.

    Some correction for his answer:

    The UIImage can itself be null (and therefore UIImage.CGImage will also be null)

    This is not true, if UIImage is null, UIImage.CGImage will throw NullReferenceException (will not return null).

    Also in Xamarin.iOS it's possible to get null CGImage if UIImage has been disposed:

    UIImage image = UIImage.FromFile(name);
    image.Dispose();
    // now image.CGImage is null