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?
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?
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
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 benull
(and thereforeUIImage.CGImage
will also benull
)
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