【问题标题】:How to Find the Image That is not Found on Ios如何找到在ios上找不到的图像
【发布时间】:2017-05-18 11:16:10
【问题描述】:

当我重新打开应用程序以查看图像时,出现以下错误:

Could not initialize an instance of the type 'UIKit.UIImage': the native 'initWithContentsOfFile:' method returned nil

是否可以通过设置忽略这个条件

MonoTouch.ObjCRuntime.Class.ThrowOnInitFailure

假的?

下面的方法用于显示图像

private async Task DisplayImageFromImageUrl()
{
 UIImage image;
 image = new UIImage(eventImageEntity.EventsImageUrl);
}

在执行上述代码时,“image”显示“null”值。

【问题讨论】:

  • 请添加失败的代码。否则,将很难帮助您解决手头的问题。

标签: xamarin.ios


【解决方案1】:

如果您尝试从 URL 创建图像,则需要使用以下代码:

                       if (!string.IsNullOrEmpty(eventImageEntity.EventsImageUrl.Trim()))
                        {
                            var url = new NSUrl(eventImageEntity.EventsImageUrl);
                            using (var data = NSData.FromUrl(url))
                            {
                               UIImage image = UIImage.LoadFromData(data);
                            }
                        }

否则你最终会得到一个 Null UIImage。这就是导致您的问题的原因。

编辑:

如果您尝试从资源位置获取图像,则需要执行以下操作:

这假设它来自特定的用户创建的文件夹:

UIImage image = UIImage.FromFile(@"Assets/NavigationBarAssets/HomePC.png")

这假设图像只是在 iOS 'Resources' 文件夹的根目录中说的

UIImage image = new UIImage("HomePC.png")

【讨论】:

  • 感谢@Digitalsa 1nt,我正在从相对路径获取图像,同时将该路径转换为 ​​UIImage 它显示空值。
  • 您在 OP 中列出的代码建议您尝试从 URL(例如 www.google.com/webenerds.png)创建图像,如果不是这种情况并且您是从文件路径执行的,或资源,那么我们理想情况下需要查看您为此使用的代码,因为它将(应该)不同。
  • 从文件路径获取图片,如:"Images/12234-1443-435-534535.png" 格式
  • @Prasanthi 我已经更新了答案以涵盖从 iOS 资源文件夹或特定用户创建的文件夹加载。希望这会更有意义。
  • 我正在初始化 UIImage image=new UIImage(eventImageEntity.EventsImageUrl) EventImageUrl 包含图像
猜你喜欢
  • 2020-09-22
  • 1970-01-01
  • 1970-01-01
  • 2016-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-03
  • 1970-01-01
相关资源
最近更新 更多