Cocoapods: resource_bundle and xcassets

Created on 16 Mar 2014  路  6Comments  路  Source: CocoaPods/CocoaPods

So, im bundling some images with a library we are building.

In my podspec i have s.resource_bundle { 'MyKit' => 'MyKit/Resources/Images.xcassets' }

How should i access my images inside MyKit ? I managed to access them writing the full-path, this is horrible because it doesn't support multiple resolution handling.

Is it possible to have different resource_bundle in subspecs?

Most helpful comment

So I've run into this problem as well, and now coming back with a solution.
I just tried adding .xcassets to the resource_bundle, and it seems to be functional now. I have a few images (each with sizes 1x,2x,3x) in the .xcassets and they successfully load.

MyKit.podspec
s.ios.resource_bundle = { 'MyKit' => 'Resources/**/*.xcassets' }
MyKitImageRetriever.swift
// or any other class within your pod
let podBundle = NSBundle(forClass: MyKitImageRetriever.self) // or any other class within the pod. technically doesn't have the be the same as the current file, but good practice to
if let url = podBundle.URLForResource("MyKit", withExtension: "bundle"){   // leave the extension as "bundle"
    let mykitBundle = NSBundle(URL: url)
    let retrievedImage = UIImage(named: "theimagename", inBundle:mykitBundle, compatibleWithTraitCollection: nil)
    // now you have the image
}

update:

sources:
1) https://guides.cocoapods.org/syntax/podspec.html#resource_bundles
2) https://github.com/CocoaPods/CocoaPods/issues/2408
3) my own cocoapod project DopamineKit

All 6 comments

Closed for duplicating CocoaPods/CocoaPods#1690 by @confidenceJuice

+1 the linked issue doesn't really seem to answer this issue. @bilby91 did you get it working in the end?

Any ideas?

+1. This still seems to be an issue.

So I've run into this problem as well, and now coming back with a solution.
I just tried adding .xcassets to the resource_bundle, and it seems to be functional now. I have a few images (each with sizes 1x,2x,3x) in the .xcassets and they successfully load.

MyKit.podspec
s.ios.resource_bundle = { 'MyKit' => 'Resources/**/*.xcassets' }
MyKitImageRetriever.swift
// or any other class within your pod
let podBundle = NSBundle(forClass: MyKitImageRetriever.self) // or any other class within the pod. technically doesn't have the be the same as the current file, but good practice to
if let url = podBundle.URLForResource("MyKit", withExtension: "bundle"){   // leave the extension as "bundle"
    let mykitBundle = NSBundle(URL: url)
    let retrievedImage = UIImage(named: "theimagename", inBundle:mykitBundle, compatibleWithTraitCollection: nil)
    // now you have the image
}

update:

sources:
1) https://guides.cocoapods.org/syntax/podspec.html#resource_bundles
2) https://github.com/CocoaPods/CocoaPods/issues/2408
3) my own cocoapod project DopamineKit

UIImageconvenience init for swift 4:

 extension UIImage {
    convenience init?(podAssetName: String) {
        let podBundle = Bundle(for: ConfettiView.self)

        /// A given class within your Pod framework
        guard let url = podBundle.url(forResource: "CryptoContribute",
                                      withExtension: "bundle") else {
            return nil

        }

        self.init(named: podAssetName,
                  in: Bundle(url: url),
                  compatibleWith: nil)
    }
 }
Was this page helpful?
0 / 5 - 0 ratings