calayer – 无法在Swift中释放由CGPathCreateMutable创建的路径

calayer – 无法在Swift中释放由CGPathCreateMutable创建的路径,第1张

概述我在 Swift中有这个简单的代码: override var bounds : CGRect { didSet { if (bounds != oldValue) { var path = CGPathCreateMutable() CGPathAddEllipseInRect(path, nil, bounds) self.path = path 我在 Swift中有这个简单的代码:

overrIDe var bounds : CGRect {  dIDSet {    if (bounds != oldValue) {      var path = CGPathCreateMutable()      CGPathAddEllipseInRect(path,nil,bounds)      self.path = path      CGPathRelease(path)    }  }}

当图层的边界发生变化时,它应该绘制一个填充图层的圆.

这是从我的旧Objective-C代码移植而来的,它运行正常:

- (voID)setBounds:(CGRect)bounds{  if (CGRectEqualToRect(self.bounds,bounds)) return;  super.bounds = bounds;  CGMutablePathref path = CGPathCreateMutable();  CGPathAddEllipseInRect(path,bounds);  self.path = path;  CGPathRelease(path);}

但是,Swift代码崩溃了一些段错误.如果我不释放路径,那很好.如果我没有设置self.path = path,它也没关系(虽然没有任何东西会显示出来).如果两者都存在,它将崩溃.

我确实认为CGPathCreateMutable()创建路径需要释放..而ObjC似乎就是这种情况.有谁知道为什么它在Swift中不起作用?或者我做错了什么?

谢谢!

解决方法 您不需要在Swift中释放CF对象.在您的情况下,CGPathCreateMutable()返回一个CGPath对象(不是CGPathref),并且内存管理与任何swift对象相同.他们在WWDC视频 Swift Interoperability In Depth中对此进行了解释 总结

以上是内存溢出为你收集整理的calayer – 无法在Swift中释放由CGPathCreateMutable创建的路径全部内容,希望文章能够帮你解决calayer – 无法在Swift中释放由CGPathCreateMutable创建的路径所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1009560.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存