Search code examples
iosuiimagecocoapodsnsbundleios-frameworks

How to embedded Images and use those Images when creating custom cocoapod in iOS, swift 4


WHAT I DID.

I'm creating my own cocoapod Library. inside my library there are two directories Classes and Assets. I put all my .swift files into Classess directory and I can access those classes properly. Then I tried to add images by adding images into Assets directory. Then it creates a another Resources folder for me and inside that folder I can see all my .png files.

WHAT I EXPECTED

now I'm trying to access those images from my .swift files. as an example thinks that I have myviewcontroller.swif file which has UIImageView inside it. then I tried :

myimageView.Image = UIImage(named:"myimage.png")

This gives me a nil value.

MORE DETAILS :

here is my podspec file.

Pod::Spec.new do |s|
    s.name             = 'bottlenecLibrary'
    s.version          = '0.1.6'
    s.summary          = 'palm ios sdk for developers'
    s.swift_version    = '4.0'

# This description is used to generate tags and improve search results.
#   * Think: What does it do? Why did you write it? What is the focus?
#   * Try to keep it short, snappy and to the point.
#   * Write the description between the DESC delimiters below.
#   * Finally, don't worry about the indent, CocoaPods strips it!

s.description      = <<-DESC
This libraray includes all necessary resources for developers
DESC

s.homepage         = 'https://bitbucket.org/palm/palmiossdk'
# s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license          = { :type => 'MIT', :file => 'LICENSE' }
s.author           = { 'palm group' => '[email protected]' }
s.source           = { :git => 'https://[email protected]/palm/palmiossdk.git', :tag => s.version.to_s }
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

s.ios.deployment_target = '9.0'

s.source_files = 'bottlenecLibrary/Classes/**/*'

s.resource_bundles = {
    'bottlenecLibrary' => ['bottlenecLibrary/Assets/*.png', 'bottlenecLibrary/Assets/*.xcassets', 'bottlenecLibrary/Resources/*.png']
}
# s.public_header_files = 'Pod/Classes/**/*.h'
# s.frameworks = 'UIKit', 'MapKit'
 s.dependency 'Alamofire', '~> 4.7'

end

I tried with adding all these : ['bottlenecLibrary/Assets/*.png', 'bottlenecLibrary/Assets/*.xcassets', 'bottlenecLibrary/Resources/*.png'] but nothing worked.

Note : I tried to read the image using bundle also.

Hope your help with this.


Solution

  • To get images in a CocoaPod you definitely need to call it specifying the bundle:

    let bundle = Bundle(for: [A_CLASS_IN_YOUR_COCOAPOD].self)
    let bundleURL = bundle.resourceURL?.URLByAppendingPathComponent("[YOUR_BUNDLE_NAME].bundle")
    let resourceBundle = NSBundle(URL: bundleURL!)
    UIImage(named: [IMAGE_NAME_STRING], in: resourceBundle, compatibleWith: nil)
    

    [YOUR_BUNDLE_NAME] would be bottlenecLibrary in this case.

    Source: How to load resource in cocoapods resource_bundle