0

我有一个用objective C编写的基类,由于许多原因,当子类需要在drawRect中绘制时,它用于获取CGContext。

-(CGContextRef)fetchContext
{
    CGContextRef context = UIGraphicsGetCurrentContext();

    return context;
}

我现在是一个用 Swift 编写的子类,它需要做一些绘图,所以在 super.. 中要求 CGContext

    override func drawRect(rect: CGRect) {
        // Drawing code
        let bounds = self.bounds;

        let context:CGContextRef = self.fetchContext();

        CGContextSetLineWidth(context, 5.0);
        CGContextSetStrokeColorWithColor(context, UIColor.redColor().CGColor );

        CGContextBeginPath(context);

        CGContextMoveToPoint(context, 0, 0);
        CGContextAddLineToPoint(context, bounds.size.width, 0);
        CGContextAddLineToPoint(context, bounds.size.width, bounds.size.height);
        CGContextAddLineToPoint(context, 0, bounds.size.height);
        CGContextAddLineToPoint(context, 0, 0);

        CGContextStrokePath(context);

        super.drawRect( rect );
    }

问题出在线路上

    let context:CGContextRef = self.fetchContext();

这不是编译错误:

'Unmanaged<CGContext>' is not convertible to 'CGContextRef'

我很困惑为什么会这样。CGContext 在 Swift 中被别名为 CGContextRef,所以我认为这会起作用。

有人可以解释/帮助吗?

4

0 回答 0