2013-09-23 29 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類中做了一個靜態函數。 –