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

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

问题描述

我在某个邮件中列出附件的情况。每个附件具有与其相关联的NSURL,其表示附件数据的路径。在非图片附件的情况下,我想在预览的地方加载预先生成的图标缩略图(即excel文档,PDF,单词等)。

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;
}


推荐答案

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天全站免登陆