如果从文件或bundle创建UIImage , image.CGImage是否可以返回null ?
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?最满意答案
通常,如果UIImage不为null ,则CGImage也不为null 。 在某些情况下,情况并非如此( 在本回答中进行了解释 ),但这不适用于您使用的方法。
如果无法找到指定的图像, UIImage本身可以为null (因此UIImage.CGImage也将为null ) - 从文档中可以看出,返回null nil (C#中为null )。 这适用于这两种方法。
FromBundle绑定到imageNamed: - docs here
FromFile绑定到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 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更多推荐
发布评论