IBDesignable类绘图UIImage不显示在情节提要中

kaz*_*ton 5 xcode cocoa storyboard swift

我已经在XCode中设计了一个自定义控制器,该控制器根据其所处的状态绘制自身。在一种状态下,该控制器通过myImage?.draw(in: rect)在覆盖的draw(_ rect: CGRect)函数中调用来显示图像。

图像正确显示在应用程序中,但未显示在情节提要中。为了说明这一点,我在同一绘制函数内添加了一个Bezier环,该环在应用程序和情节提要中均显示:

应用中显示的控制器

情节提要中显示的控制器

在研究了问题之后,我相信这可能与图像是可选图像有关。但是,将其显式展开(即,调用myImage!.draw(in: rect)会导致情节提要自动布局崩溃:

error: IB Designables: Failed to render and update auto layout status for SermonViewController (1zk-bD-8it): The agent crashed
Run Code Online (Sandbox Code Playgroud)

我已经删除了该类的完整实现:

import UIKit

@IBDesignable class DownloadIndicatorControl: UIView {

    let requestDownloadImage = UIImage(named: "requestDownloadImage")
    let lineWidth: CGFloat = 3.0
    override func draw(_ rect: CGRect) {

        // Draw Image
        requestDownloadImage?.draw(in: rect)

        // Draw ring around image
        let radius: CGFloat = fmin(self.bounds.width, self.bounds.height) / 2 - lineWidth / 2
        let center: CGPoint = CGPoint(x: self.bounds.size.width / 2, y: self.bounds.size.height / 2)
        let disk: UIBezierPath = UIBezierPath()
        disk.lineWidth = lineWidth
        disk.addArc(withCenter: center,
                    radius: radius,
                    startAngle: CGFloat(-Double.pi / 2),
                    endAngle: CGFloat(2 * Double.pi - Double.pi/2),
                    clockwise: true)
        disk.stroke()
    }

    override init(frame: CGRect){
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}
Run Code Online (Sandbox Code Playgroud)

我可以做些什么来使图像显示在情节提要中,并且有什么方法可以明确地展开图像而不会使情节提要不断崩溃?

Sha*_*hen 5

UIImage(named :)方法使用主捆绑包,但是Interface Builder以不同的方式加载资源。

尝试这个:

UIImage(name:in:compatibleWith :)

import UIKit

@IBDesignable class DownloadIndicatorControl: UIView {

var requestDownloadImage: UIImage?
let lineWidth: CGFloat = 3.0

override func draw(_ rect: CGRect) {

    // Draw Image
    requestDownloadImage?.draw(in: rect)

    // Draw ring around image
    let radius: CGFloat = fmin(self.bounds.width, self.bounds.height) / 2 - lineWidth / 2
    let center: CGPoint = CGPoint(x: self.bounds.size.width / 2, y: self.bounds.size.height / 2)
    let disk: UIBezierPath = UIBezierPath()
    disk.lineWidth = lineWidth
    disk.addArc(withCenter: center,
                radius: radius,
                startAngle: CGFloat(-Double.pi / 2),
                endAngle: CGFloat(2 * Double.pi - Double.pi/2),
                clockwise: true)
    disk.stroke()
}

override init(frame: CGRect){
    super.init(frame: frame)
    self.setup()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    self.setup()
}

override func prepareForInterfaceBuilder() {
    super.prepareForInterfaceBuilder()
    self.setup()
}

internal func setup() {
    let bundle = Bundle(for: DownloadIndicatorControl.self)
    requestDownloadImage = UIImage(named: "download", in: bundle, compatibleWith: nil)
}
}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明