2017-06-22 30 views
1

我已经创建了Cocoa Touch基础应用程序,并尝试以编程方式加载图像。我已将我的图像放入捆绑资源中。通过故事板图片被加载,但是,不能以编程方式工作。图像不会显示出来。如何在Cocoa Touch框架中加载图像?

+0

您需要正确获取框架包。 – Bala

+0

如果我通过文件检查器选择是否在故事板中加载图像。但如果我通过编程方式完成相同的工作。 imageView.image = UIImage(named:“image.jpg”) – vara

回答

2

我们正在创建在另一个项目中使用的框架,因此在您的框架中,您想访问您的frameWork捆绑包,通过以下代码获取frameWork捆绑包。

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) 
+0

我需要在框架中做什么? – vara

+0

是否在inDirectory:fields必须? – vara

+0

是的,在将您的框架集成到任何项目时,它将位于该目录下。试试吧,让我知道 – Bala