我可以从 XCAssets 包中获取 NSURL 吗? [英] Can I get a NSURL from an XCAssets bundle?

查看:17
本文介绍了我可以从 XCAssets 包中获取 NSURL 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了列出邮件中的附件的情况.每个附件都有一个与之关联的 NSURL,表示附件数据的路径.对于非图像附件,我想加载预先生成的图标缩略图以代替预览(即 excel 文档、PDF、word 等).

I'm in a situation where I am listing attachments from a message. Each attachment has a NSURL associated with it that represents a path to the attachment data. In the case of non-image attachments I want to load pre-generated icon thumbnails in the place of a preview (i.e. excel documents, PDF, word, etc).

我目前在我的项目中拥有这些图像,位于 XCAssets 包中.我可以使用 [UIImage imageNamed:@"someName"] 找到它们.我似乎无法通过 NSBundle 的各种资源查找方法找到它们.XCAssets 包是否会以某种方式更改我正在寻找的图标图像的文件名?

I currently have these images in my project, within an XCAssets bundle. I can get at them using [UIImage imageNamed:@"someName"]. I cannot seem to get at them via NSBundle's various resource lookup methods. Does the XCAssets bundle somehow change the filenames of the icon images I'm looking for?

这是我目前正在使用的代码.路径每次都为零.在这种情况下我不需要使用 XCAssets 吗?

Here's the code I'm currently working with. Path is nil every time. Do I need to not use XCAssets in this case?

+ (NSURL *)mediaURLWithMessage:(SRMediaMessage*)message
{
  NSURL *url = message.mediaURL;

  // if this is an image URL or non-existant, there is no work left to do, return it.
  if (!url || (message.secureFile.secureFileMimeType & SRSecureFileMimeTypeImage))
    return url;

  NSString *filename = @"unknown";

  switch (message.secureFile.secureFileMimeType)
  {
    case SRSecureFileMimeTypeDOC:
      filename = @"doc";
      break;

    case SRSecureFileMimeTypePPT:
      filename = @"ppt";
      break;

    case SRSecureFileMimeTypePDF:
      filename = @"pdf";
      break;

    case SRSecureFileMimeTypeXLS:
      filename = @"exl";
      break;

    case SRSecureFileMimeTypeCSV:
      filename = @"csv";
      break;

    case SRSecureFileMimeTypeTXT:
      filename = @"txt";
      break;

    case SRSecureFileMimeTypeRTX:
      filename = @"rtf";
      break;

    default:
    case SRSecureFileMimeTypeMP4:
      // unknown icon for now.
      break;

      // unused but available:
//      @"ilife"

  }

  NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:nil];
  if (path)
    url = [NSURL fileURLWithPath:path];

  return url;
}

推荐答案

如果您的目标是 iOS 7+,Xcode 5 现在会将资产放入新的文件格式.1 个文件用于所有资产.这意味着您无法直接访问该文件.

If you're targeting iOS 7+, Xcode 5 now puts the assets into a new file format. 1 file for all of the assets. This means you can not get access to the file directly.

如果您需要直接访问该文件,您可以将其作为资产目录之外的普通图像包含在内.

If you need to access the file directly, you can include it as an normal image outside of an asset catalog.

这篇关于我可以从 XCAssets 包中获取 NSURL 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆