从文件创建的CGImage可以为null(Can CGImage created from file be null)

系统教程 行业动态 更新时间:2024-06-14 17:04:03
从文件创建的CGImage可以为null(Can CGImage created from file be null)

如果从文件或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

更多推荐

本文发布于:2023-04-24 21:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/edd5846a5a530f3ce8b43fc1ba6dc37d.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   CGImage   null   created   file

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!