ARC 可以与 Core Graphics 对象一起使用吗?
我最近开始了一个使用自动引用计数 (ARC) 的新项目。
当我分配 CALayer 的内容时:
UIView* view = ...
UIImage* image = ...
view.layer.contents = image.CGImage
我收到错误
ARC 不允许将非 Objective-C 指针类型“CGImageRef”隐式转换为“id”
简单地将 CGImageRef
转换为 id
会隐藏错误,但我想知道那么 ARC 是否还能正常工作呢?
I recently started a new project using Automatic Reference Counting (ARC).
When I assigned the contents of a CALayer:
UIView* view = ...
UIImage* image = ...
view.layer.contents = image.CGImage
I got an error
Implicit conversion of a non-Objective-C pointer type 'CGImageRef' to 'id' is disallowed with ARC
Simply casting the CGImageRef
to id
hides the error, but I was wondering if the ARC still functions correctly then?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您确实应该查看 WWDC 2011 的 ARC 视频。它们可以在开发者网站上找到并通过 iTunes 打开。尤其:
另外,ARC 参考说明:
参考说明和视频都讨论了 Core Graphics(等)以及它们如何使用弧。
具体来说,请查看“管理免费桥接”部分
Jörg Jacobsen 对桥接选项也有很好的总结概述:在 ARC 环境中管理免费桥接。
You should really check out the ARC videos from WWDC 2011. They are available on the developer site and open through iTunes. Especially:
Also, the ARC reference notes:
Both the reference notes and the videos discuss Core Graphics (et al) and how they work with ARC.
Specifically, look at the section called "Managing Toll-Free Bridging"
Jörg Jacobsen has a good summary overview of the bridging options as well: Managing Toll-free Bridging in an ARC’ed Environment.
尽管史蒂夫指出了参考文献,但我相信您上面展示的案例可能很特殊。来自过渡到 ARC 发行说明,注意“编译器处理从 Cocoa 方法返回的 CF 对象”一节:
他们提供的代码示例:
依赖于这些方法已知的 CGColors 返回(它们缺少我在上面代码中添加的 id 的转换,这应该很快在他们的文档中得到纠正)。
因为
[image CGImage]
遵循命名约定,所以我相信 CGImage 会在这里正确桥接。我认为你的 id 转换应该就是你所需要的。Despite the references pointed out by Steve, I believe the case you show above might be special. From the Transitioning to ARC Release Notes, pay attention to the section "The Compiler Handles CF Objects Returned From Cocoa Methods":
The code example they provide:
relies on the known return of CGColors from these methods (they are missing the cast to id that I've added in the above code, which should be corrected in their documentation soon).
Because
[image CGImage]
follows the naming conventions, I believe the CGImage will be bridged properly here. I think that your cast to id should be all that you need here.对于
layer.contents = (id)image.CGImage
问题的一个流行答案是layer.contents = obj_unretainedObject(image.CGImage)
。我做
=(__bridge id)image.CGImage
。One popular answer to
layer.contents = (id)image.CGImage
question islayer.contents = obj_unretainedObject(image.CGImage)
.I do
=(__bridge id)image.CGImage
.