简体   繁体   中英

How to load images in a Cocoa Touch framework?

I have created the Cocoa Touch base app and have tried to load images programmatically. I have placed my images in the bundle resources. Through storyboards the images are loaded, however, but is does not work programmatically. The images won't show up.

We are creating framework for using in another projects, So in your framework you want to access your frameWork bundle get the frameWork bundle by below code.

func getBundle() -> Bundle? {
    var bundle: Bundle?
    if let urlString = Bundle.main.path(forResource: "YOUR_FRAMEWORK_BUNDLE_NAME", ofType: "framework", inDirectory: "Frameworks") {
        bundle = (Bundle(url: URL(fileURLWithPath: urlString)))
    }
        return bundle
}


let bundel = getBundle()

imageView.image = UIImage(named: "image.jpg", in: bundel, compatibleWith: nil)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM