Ios 如何在Cocoa Touch框架中加载图像?

Ios 如何在Cocoa Touch框架中加载图像?,ios,swift,bundle,cocoa-touch,Ios,Swift,Bundle,Cocoa Touch,我创建了Cocoa Touch base应用程序,并尝试以编程方式加载图像。我已将图像放在捆绑资源中。但是,通过故事板可以加载图像,但它不能以编程方式工作。图像将不会显示。我们正在创建用于其他项目的框架,因此在您的框架中,您希望访问您的框架包通过以下代码获取框架包 func getBundle() -> Bundle? { var bundle: Bundle? if let urlString = Bundle.main.path(forResource: "YOUR_F

我创建了Cocoa Touch base应用程序,并尝试以编程方式加载图像。我已将图像放在捆绑资源中。但是,通过故事板可以加载图像,但它不能以编程方式工作。图像将不会显示。

我们正在创建用于其他项目的框架,因此在您的框架中,您希望访问您的框架包通过以下代码获取框架包

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)

您需要正确获取框架包。如果我通过文件检查器选择,则图像是否加载到故事板中。但这和我通过编程实现的一样。imageView.image=UIImage(名为:“image.jpg”)inDirectory:字段是否必须?是的,在将框架集成到任何项目时,它将位于该目录下。尝试一下,让我知道您正在创建框架或应用程序?让我们来看看。谢谢,它工作得很好@巴拉