2016-02-29 4 views
10

私はcocoapods resource_bundleでリソースを読み込む方法に苦労しました。cocoapods resource_bundleでリソースを読み込む方法

以下は、.podspecsファイルに記載したものです。

s.source_files = 'XDCoreLib/Pod/Classes/**/*' 
s.resource_bundles = { 
'XDCoreLib' => ['XDCoreLib/Pod/Resources/**/*.{png,storyboard}'] 
} 

これは私がメインプロジェクトからやろうとしていることです。

let bundle = NSBundle(forClass: XDWebViewController.self) 
let image = UIImage(named: "ic_arrow_back", inBundle: bundle, compatibleWithTraitCollection: nil) 
print(image) 

私はXDCoreLib.bundleの画像を見ましたが、それはゼロを返します。

+0

を、単にこれらのリソースをコピーすることをポッドで、これらのリソースができるため、プロジェクトにしないでくださいいつでも更新/削除できます。 –

+1

このポッドは自分で管理しています。プロジェクトの規模はますます大きくなり、各モジュールを分離したいと考えています。 –

答えて

16

私はしばらくの間、同様の問題で苦労しました。ポッド用のリソースバンドルは、実際にコードが存在する場所とは別のバンドルです。次のことを試してください:あなたは私のポッドにXIBとのplistファイルを使用したとき、私はそれに直面しcocoapodライブラリ でもあるソースファイルでそれを行う必要があるとき

let frameworkBundle = NSBundle(forClass: XDWebViewController.self) 
let bundleURL = frameworkBundle.resourceURL?.URLByAppendingPathComponent("XDCoreLib.bundle") 
let resourceBundle = NSBundle(URL: bundleURL!) 
let image = UIImage(named: "ic_arrow_back", inBundle: resourceBundle, compatibleWithTraitCollection: nil) 
print(image) 
+0

素晴らしい! "URLByAppending"がありません。ありがとうございます。 –

+0

@Quaid XDWebViewController.selfとは何ですか? – Siddh

+0

@Siddh 'XDWebViewController.self'は、ポッド内のクラスにする必要があります。 –

0

興味深い事があります。

これは次の方法で解決されました。 xibファイルの読み込みの例:

let bundle = Bundle(for: self.classForCoder) 
let viewController = CocoapodViewController(nibName: "CocoapodViewController", bundle: bundle) 
1

私は他の答えのいずれかがPodspecとの関係を記述していないと思います。バンドルURLは、ポッドの名前を使用し、.bundleを使用してリソースバンドルを検索します。この方法は、ポッドの内側と外側の両方のポッド画像にアクセスするためのアセットカタログで機能します。

// grab bundle using `Class` in pod source (`self`, in this case) 
NSBundle *bundle = [NSBundle bundleForClass:self.classForCoder]; 
NSURL *bundleURL = [[bundle resourceURL] URLByAppendingPathComponent:@"PodName.bundle"]; 
NSBundle *resourceBundle = [NSBundle bundleWithURL:bundleURL]; 
UIImage *podImage = [UIImage imageNamed:@"image_name" inBundle:resourceBundle compatibleWithTraitCollection:nil]; 

スウィフト

Podspec

Pod::Spec.new do |s| 
    s.name    = 'PodName' 
    s.resource_bundles = { 
    'ResourceBundleName' => ['path/to/resources/*/**'] 
    } 
end 

のObjective-C

this answerを参照してください。

0

私はCocoaPodsライブラリに格納されたカテゴリからNIBを開きたいと思っていました。もちろん[self classForCoder]UIKitを返しました(カテゴリのため)ので、上記の方法はうまくいきませんでした。

私はいくつかのハードコードされたパスを使用して問題を解決:

NSURL *bundleURL = [[[NSBundle mainBundle] resourceURL] URLByAppendingPathComponent:@"Frameworks/MyCocoaPodLibraryName.framework"]; 
NSBundle *podBundle = [NSBundle bundleWithURL:bundleURL]; 

CustomView *customView = [[podBundle loadNibNamed:@"CustomView" owner:self options:nil] firstObject]; 
2

これは、(その後pod installs.resource_bundlesに加えてs.resourcesを追加.podspec

に動作します

s.resources = 'XDCoreLib/Pod/Resources/**/*.{png,storyboard}' 

次に、あなたのコード内で、そのように簡単:

let image = UIImage(named: "image_name.png", in: Bundle(for: type(of: self)), compatibleWith: nil) 
0

Podsプロジェクトを選択して目的のターゲットを選択し、[全般]タブにいることを確認するだけで、バンドルのIDを確認できます。

enter image description here

その後のコードで、あなたはとても似ているポッド内の画像を言う読み込むことができます。

backgroundImageView.image = UIImage(named: "business_fellas", in: Bundle(identifier: "org.cocoapods.StandardLibrary3-0"), compatibleWith: nil) 
関連する問題