访问资产目录pathForResource

时间:2013-09-23 20:35:22

标签: ios7 xcode5 nsbundle

看来:

[[NSBundle mainBundle] pathForResource:@"name" ofType:@"png"];

不为Images.xcassets资产目录中的资产返回任何内容。我也尝试过:

[[NSBundle mainBundle] pathForResource:@"name" ofType:@"png" inDirectory:@"Images"];

[[NSBundle mainBundle] pathForResource:@"name" ofType:@"png" inDirectory:@"Images.xcassets"];

但这两个人都没有。

有没有人成功检索目录中资产的路径?

6 个答案:

答案 0 :(得分:25)

同样的问题,但我认为我们不能通过pathForResource:访问它,除了下面的内容:

UIImage* image = [UIImage imageNamed:@"name-in-asset-catalog"];

显然documented

  

资产目录中的每个集都有一个名称。您可以使用该名称   以编程方式加载集合中包含的任何单个图像。至   加载图像,调用UIImage:imageNamed:方法,传递名称   包含图像的集合。

我发现在编译应用程序包中,出现了一个名为“Assets.car”的文件,我认为这是我项目中的整个图像集,应该是压缩的

请参阅Apple的文档:

  

Xcode 5为资产目录提供不同的功能   在您的项目的部署目标上:

     
      
  • 对于所有项目,可以使用集名称加载单个图像。
  •   
  • 对于部署目标为iOS 7的项目,Xcode会编译您的资产   将目录编入运行时二进制文件格式,以减少下载   你的应用程序的时间。
  •   

就是这样。

我也在寻找一种不使用imageNamed:的方法,我不希望运行时缓存我的图像。

答案 1 :(得分:11)

[UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.png", [[NSBundle mainBundle] resourcePath], imgName]];

imgName:图像集的名称,不关心图像集中图像的真实名称。

答案 2 :(得分:11)

我想访问一些使用本地资源创建UNNotificationAttachment的矢量资产,所以我想出了这个帮助类。它基本上只是从资产中获取图像,将其数据保存到磁盘并返回文件URL。我希望能帮助别人。

import UIKit

class AssetExtractor {

    static func createLocalUrl(forImageNamed name: String) -> URL? {

        let fileManager = FileManager.default
        let cacheDirectory = fileManager.urls(for: .cachesDirectory, in: .userDomainMask)[0]
        let url = cacheDirectory.appendingPathComponent("\(name).png")

        guard fileManager.fileExists(atPath: url.path) else {
            guard
                let image = UIImage(named: name),
                let data = UIImagePNGRepresentation(image)
            else { return nil }

            fileManager.createFile(atPath: url.path, contents: data, attributes: nil)
            return url
        }

        return url
    }

}

答案 3 :(得分:2)

尝试使用“编译资产目录”步骤中确定的图像名称。你会在构建输出中找到它。一定要扩大成绩单 - 你应该看到这样的东西:

/* com.apple.actool.compilation-results */
/path/to/Debug-iphonesimulator/Your.app/LaunchImage-800-Portrait-736h@3x.png
/path/to/Debug-iphonesimulator/Your.app/LaunchImage-800-667h@2x.png
/path/to/Debug-iphonesimulator/Your.app/LaunchImage-700@2x.png
/path/to/Debug-iphonesimulator/Your.app/LaunchImage-700-568h@2x.png
/path/to/Debug-iphonesimulator/Your.app/LaunchImage-700-Landscape~ipad.png
/path/to/Debug-iphonesimulator/Your.app/LaunchImage-700-Landscape@2x~ipad.png

使用+[UIImage imageNamed:]使用Xcode 6.1.1进行测试。

答案 4 :(得分:1)

虽然有一个Contents.json文件(见下文)与xcassets中包含该项目图像的文件名的每个项目相关联,但它似乎无法在文件系统级别访问。编译时,所有图像都放在一个文件系统文件夹中。 json文件是自动生成的,不应编辑,但它确实为解决方法提供了模板。

使用一致后缀命名每个文件,以匹配json文件中出现的相应习语,比例和子类型。这需要通过为每个资产选择“在Finder中显示”选项并重命名每个文件来进行手动操作,但是一旦完成,您就可以将pathForResource与函数结合使用,将相应的后缀添加到基础资产名称中,以便适当地检索 - 大小的图像。

检索路径的示例代码:

NSString *imageName = @"Add to Cart";
NSString *imageSuffix = [self someMethodThatDeterminesSuffix]; // Example: "-iphone-2x-retina4"
NSString *imagePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@%@", imageName, imageSuffix] ofType:@"png"];

“添加到购物车”图片资产的示例Contents.json文件:

{
  "images" : [
    {
      "idiom" : "iphone",
      "scale" : "1x",
      "filename" : "Add to Cart-iphone-1x.png"
    },
    {
      "idiom" : "iphone",
      "scale" : "2x",
      "filename" : "Add to Cart-iphone-2x.png"
    },
    {
      "idiom" : "iphone",
      "filename" : "Add to Cart-iphone-2x-retina4.png",
      "subtype" : "retina4",
      "scale" : "2x"
    }
  ],
  "info" : {
    "version" : 1,
    "author" : "xcode"
  }
}

答案 5 :(得分:0)

特别是对于启动图像和应用程序图标,图像路径分别通过UILaunchImages和CFBundleIcons Info.plist键提供。在使用资产目录时,看起来这些图像是单独生成的,并且在应用程序构建期间更新了Info.plist值(如果不是,则这些键由Xcode UI直接写入)。您可能必须编写一些代码来确定要使用的正确子字典,但图像可以单独使用,在这种情况下您可以避免使用imageNamed。

相关问题