0

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?
Kirill
  • 7,580
  • 6
  • 44
  • 95

2 Answers2

1

In general, if the UIImage is not null, the CGImage will also be not null. There are a few cases where this is not the case (explained in this answer), but this would not apply to the methods you are using.

The UIImage can itself be null (and therefore UIImage.CGImage will also be null) if the specified image could not be found - As you can see from the docs, the return will be nil (null in C#). This applies to both methods.

FromBundle is bound to imageNamed: - docs here

FromFile is bound to imageWithContentsOfFile: - docs here

Community
  • 1
  • 1
BytesGuy
  • 4,097
  • 6
  • 36
  • 55
0

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
Kirill
  • 7,580
  • 6
  • 44
  • 95