Swift-Pod images not loading (not found, returns zero)

Ive created a new pod component following the instructions on the CocoaPods website. Now I have the following structure inside my Pods:

  • Subscriptions (folder)

    • MyComponent (folder)

      • Mycompponent.swift

      • Resources (folder)

        • name_of_image.png

However, when I try to call:

UIImage(named: "name_of_image")

from MyComponent.swift, I get a "zero" return.

Ive been reading solutions that include creating a package for my component, but I have not found the right way to generate it. Any advice would be nice.

thanks

+4
source share
2 answers

You cannot (should not) use UIImage(named:)inside Pod. This will download the image from your application, not from Pod.

Pod NSBundle, . NSBundle, Pod.

let bundle = NSBundle(forClass: ClassFromYourPod.self)
let image = UIImage(named: "xyz", inBundle: bundle, compatibleWithTraitCollection: nil)
+6

, png xcassets

    let bundle = Bundle(for: ClassInsidePod.self)
    let image = UIImage(named: "YourImageName", in: bundle, compatibleWith: nil)
    imageView.image = image

, Example, pod update. , , nil

0

Source: https://habr.com/ru/post/1617896/


All Articles