如何使用 IBDesignable UIImageView 在 prepareForInterfaceBuilder 中加载图像

How to load an image in prepareForInterfaceBuilder with a IBDesignable UIImageView(如何使用 IBDesignable UIImageView 在 prepareForInterfaceBuilder 中加载图像)

本文介绍了如何使用 IBDesignable UIImageView 在 prepareForInterfaceBuilder 中加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 IB 可设计的 UIImageView 中加载示例图像,以便在编辑界面时显示在 Interface Builder 中.以下代码不起作用,因为 IB 中的视图占位符仍然为空(视图区域仅包含 UIImageView 文本):

I would like to load a sample image in an IB designable UIImageView, to be shown in Interface Builder while editing the interface. The following code does not work, as the view placeholder in IB remains empty (the view area contains only the UIImageView text):

@IBDesignable
class TestImageView : UIImageView
{
    override func prepareForInterfaceBuilder() {
        //let bundle = NSBundle.mainBundle()
        let bundle = NSBundle(forClass: nil)
        let imagePath = bundle.pathForResource("Test", ofType: "jpg")
        self.image = UIImage(contentsOfFile: imagePath)
    }
}

注意:

  • 在 IB 中,视图的自定义类是正确的 (TestImageView)
  • Test.jpg 存在于项目中(如果我在 IB 中手动设置 UIImageView 的图像属性,图像就会显示出来).
  • 我尝试了两种不同的方法来获取代码中存在的包
  • in IB the Custom Class class for the view is correct (TestImageView)
  • Test.jpg is present in the project (if I manually set the image property of the UIImageView in IB the image shows up).
  • I tried the two different methods of getting the bundle present in the code

这是使用 Xcode 6 beta 3 测试的.

This was tested with Xcode 6 beta 3.

更新:在这两种情况下,我得到的包路径都是 "/Applications/Temporary/Xcode6-Beta3.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Xcode/叠加层".在该路径中,图像显然不存在.

Update: in both cases the bundle path I get is "/Applications/Temporary/Xcode6-Beta3.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Xcode/Overlays". In that path the image is obviously not present.

推荐答案

为 Swift 4.2 更新

当您实例化 UIImage(使用 UIImage(named : "SomeName"))时,应用程序将在您的 主包 中查找资产,这通常可以正常工作.但是当您在 设计时,InterfaceBuilder 将可设计视图的代码(用于编译同时设计)在一个单独的包中.

Updated for Swift 4.2

When you instantiate an UIImage (with UIImage(named : "SomeName") the app will look for the asset in your main bundle, which works fine usually. But when you are at design time, the InterfaceBuilder holds the code of the designable views (for compiling while designing) in a separate bundle.

因此解决方案是:动态定义您的捆绑包,因此您的文件可以在设计、编译和运行时找到:

So the solution is: Define your bundle dynamically, hence your files can be found in design, compile and run time:

    // DYNAMIC BUNDLE DEFINITION FOR DESIGNABLE CLASS
    let dynamicBundle = Bundle(for: type(of: self))

    // OR ALTERNATIVELY BY PROVDING THE CONCRETE NAME OF YOUR DESIGNABLE VIEW CLASS
    let dynamicBundle = Bundle(for: YourDesignableView.self)

    // AND THEN SUCCESSFULLY YOU CAN LOAD THE RESSOURCE
    let image = UIImage(named: "Logo", in: dynamicBundle, compatibleWith: nil)

这篇关于如何使用 IBDesignable UIImageView 在 prepareForInterfaceBuilder 中加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何使用 IBDesignable UIImageView 在 prepareForInterfaceBuilder 中加载图像

基础教程推荐