如何嵌入图像并在 iOS、swift 中创建自定义 cocoapod 时使用这些图像 4

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

我做了什么。

我正在创建自己的 cocoapod 库。在我的库中有两个目录 ClassesAssets。我将所有 .swift 文件放入 Classess 目录,我可以正确访问那些 类。然后我尝试通过将图像添加到 Assets 目录来添加图像。然后它为我创建了另一个 Resources 文件夹,在该文件夹中我可以看到我所有的 .png 文件。

我所期望的

现在我正尝试从我的 .swift 文件中访问这些图像。例如,我认为我有 myviewcontroller.swif 文件,其中包含 UIImageView 。然后我试了:

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

这给了我一个零值。

更多详情:

这是我的 podspec 文件。

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' => 'pods@palm.com' }
s.source           = { :git => 'https://palmss@bitbucket.org/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

我尝试添加所有这些:['bottlenecLibrary/Assets/*.png', 'bottlenecLibrary/Assets/*.xcassets', 'bottlenecLibrary/Resources/*.png'] 但没有任何效果。

注意:我也尝试使用 bundle 读取图像。

希望得到你的帮助。

要在 CocoaPod 中获取图像,您肯定需要调用它指定包:

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] 将是 bottlenecLibrary

来源: