dan*_*ing 4 calayer cgpath swift
我在Swift中有这个简单的代码:
override var bounds : CGRect {
didSet {
if (bounds != oldValue) {
var path = CGPathCreateMutable()
CGPathAddEllipseInRect(path, nil, bounds)
self.path = path
CGPathRelease(path)
}
}
}
Run Code Online (Sandbox Code Playgroud)
当图层bounds发生变化时,它应该绘制一个填充图层的圆圈.
这是从我的旧Objective-C代码移植而来的,它运行正常:
- (void)setBounds:(CGRect)bounds
{
if (CGRectEqualToRect(self.bounds, bounds)) return;
super.bounds = bounds;
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddEllipseInRect(path, nil, bounds);
self.path = path;
CGPathRelease(path);
}
Run Code Online (Sandbox Code Playgroud)
但是,Swift代码崩溃了一些段错误.如果我不释放路径,那很好.如果我没有设置self.path = path,它也没关系(虽然没有任何东西会显示出来).如果两者都存在,它将崩溃.
我确实认为通过CGPathCreateMutable()需要释放创建的路径..而且似乎是ObjC的情况.有谁知道为什么它在Swift中不起作用?或者我做错了什么?
谢谢!
您不需要在Swift中释放CF对象.在您的情况下,CGPathCreateMutable()返回一个CGPath对象(不是CGPathRef)并且内存管理与任何swift对象相同.他们在WWDC视频Swift Interoperability In Depth中对此进行了解释
| 归档时间: |
|
| 查看次数: |
2907 次 |
| 最近记录: |