CGContextRef не работает с @IBDesignable (ошибка: сбой агента)

Я создаю подкласс UIButton, чтобы добавить рисунок. Он отлично работает в коде. Это дает следующую ошибку, когда я пытаюсь использовать @IBdesignable.

  1. ошибка: IB Designables: не удалось обновить статус автоматического макета: произошел сбой агента

  2. ошибка: IB Designables: не удалось отобразить экземпляр DashboardHeaderView: произошел сбой агента

Ниже приведен пример кода

let context:CGContextRef = UIGraphicsGetCurrentContext()!
UIGraphicsPushContext(context)
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0);

var path:UIBezierPath!
let pathRect:CGRect = CGRectMake(strokeWidth / 2, strokeWidth / 2, pathSize - iconStrokeWidth, pathSize - iconStrokeWidth);
path = UIBezierPath(rect: pathRect )
iconColor.setStroke()
iconPath.lineWidth = iconStrokeWidth;
iconPath.stroke()
CGContextAddPath(context, iconPath.CGPath);

let image:UIImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsPopContext();
UIGraphicsEndImageContext();

Ошибка сбоя агента

let context:CGContextRef = UIGraphicsGetCurrentContext()! 

Я отключил init + drawRect + initilizeButtonDrawings, но ничего положительного не получил

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)!
    self.initilizeButtonDrawings()
}

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

override func drawRect(rect:CGRect) {
    super.drawRect(rect)
    self.initilizeButtonDrawings()
}
override func prepareForInterfaceBuilder () {
    self.initilizeButtonDrawings()
}

Все это отлично работает с Objective C, но падает в swift.


person MOHAMMAD ISHAQ    schedule 11.12.2015    source источник


Ответы (1)


Я решил это с помощью следующего обновления...

let rect:CGRect = CGRectMake(0, 0, iconSize, iconSize)

UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0);
let context  = UIGraphicsGetCurrentContext()
 // draw icon

var iconPath:UIBezierPath!
let iconRect:CGRect = CGRectMake(iconStrokeWidth / 2, iconStrokeWidth / 2, iconSize - iconStrokeWidth, iconSize - iconStrokeWidth);
if self.iconSquare {
    iconPath = UIBezierPath(rect:iconRect )
} else {
    iconPath = UIBezierPath(ovalInRect:iconRect)
}
iconColor.setStroke()
iconPath.lineWidth = iconStrokeWidth;
iconPath.stroke()
CGContextAddPath(context, iconPath.CGPath);
let image:UIImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
person MOHAMMAD ISHAQ    schedule 04.03.2016