【问题标题】:How to check if an image exists in Xcode Asset Catalog?如何检查 Xcode 资产目录中是否存在图像?
【发布时间】:2014-03-27 07:09:38
【问题描述】:

我正在通过 HTTP 获取 JSON 数据,并希望在带有自定义 MKAnnotationView 的 MKMapView 中显示它们。所以到这里没问题。

糟糕的是,REST 服务可能会返回在我的 .xcassets 文件中没有等效项的引脚类型。有什么方法可以检查应用程序资产目录中的条目并避免出现这样的消息:

CUICatalog:提供的资产名称无效:NonExistingImage,或比例因子无效:2.000000

当前代码示例:

NSString * imageName = @"NonExistingImage";

if (imageName != nil) {
    // this line creates the above message
    UIImage * image = [UIImage imageNamed:imageName]; 

    // checking for image != nil, otherwise use default image
} else {
    // default image
}

【问题讨论】:

  • 好问题。它可以归结为“如何检查 XCode 资产目录中是否存在图像?”。
  • 谢谢。更正了标题;)
  • 有什么理由删除该消息吗?它似乎没有害处,因为无效的资产名称只会返回 nil,没有例外。

标签: ios objective-c xcode uiimage


【解决方案1】:

我的解决方案: 似乎您使用空 NSString(或 nil)调用 imageNamed,此信息正在被记录。如果传递的图像根本不存在,则生成的 UIImage 为 nil 并创建 NO 消息。

【讨论】:

    猜你喜欢
    • 2014-08-18
    • 1970-01-01
    • 2018-11-14
    • 2015-07-19
    • 1970-01-01
    • 1970-01-01
    • 2021-02-11
    • 1970-01-01
    • 2012-09-12
    相关资源
    最近更新 更多