2013-09-23 31 views
61

看来:访问资产类别pathForResource

[[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"];

但无论是那些工作无论是。

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

+5

也有同样的问题! – vilanovi

+0

我也需要图像的实际路径,因为我不想使用imageNamed(破碎缓存),事实证明资产目录也被破坏而没有编程访问。 – Alper

+0

@MobileVet你有没有找到解决方案? – Keab42

回答

24

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

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

已清楚地documented

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

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

请参考苹果的文档:

的Xcode 5条规定,对项目的部署目标取决于 资产目录不同的功能:

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

就这样。

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

+4

我有关于在这里缓存图像相同的问题http://stackoverflow.com/questions/19919113/uiimage-caching-with-xcode-asset-catalogs#comment29693553_19919113。它看起来像imageNamed:使用资产目录时缓存不能按预期工作,因为内存永远不会被系统释放,甚至不会在内存警告中释放。 – knl

+6

我可以验证这一点。内存不释放。这部分iOS相当糟糕。 – Alper

+0

如果资产中有一个文件夹,并且我只需要此文件夹中的照片,该怎么办?请帮忙吗? –

1

尽管与包含该项目图像文件名的xcassets中的每个项目关联的Content.json文件(请参见下文)似乎并未在文件系统级别可访问。编译后,所有图像都放置在单个文件系统文件夹中。 json文件是自动生成的,不应该被编辑,但它确实为解决方法提供了一个模板。

为每个文件命名后缀一致,以匹配出现在json文件中的相应成语,缩放和子类型。这需要通过为每个资产选择“Show in 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" 
    } 
} 
+1

大挖,你试过了吗?它工作吗? – MobileVet

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

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

+1

除了主包之外,其他任何包中的图像都不能处理图像。 – user1010819

+1

当然,我在这里用户[NSBundle mainBundle]。 – Lyle

+0

谢谢,伙计。你做它的方式是+(UIImage *)imageNamed的好替代:(NSString *)name inBundle:(NSBundle *)bundle compatibleWithTraitCollection:(UITraitCollection *)traitCollection。它是在iOS 8.0中引入的,我也需要覆盖7.x.x。 –

2

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

/* com.apple.actool.compilation-results */ 
/path/to/Debug-iphonesimulator/Your.app/[email protected] 
/path/to/Debug-iphonesimulator/Your.app/[email protected] 
/path/to/Debug-iphonesimulator/Your.app/[email protected] 
/path/to/Debug-iphonesimulator/Your.app/[email protected] 
/path/to/Debug-iphonesimulator/Your.app/LaunchImage-700-Landscape~ipad.png 
/path/to/Debug-iphonesimulator/Your.app/[email protected]~ipad.png 

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

0

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

10

我想访问一些矢量资产,用本地资源创建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 
    } 

} 
+1

我有同样的问题,这很好。感谢分享。 – RommelTJ

+0

不错!我在UIImage类中做了一个静态函数。 –