ARC使用Core Graphics对象吗? [英] Does ARC work with Core Graphics objects?

查看:124
本文介绍了ARC使用Core Graphics对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近使用自动引用计数(ARC)启动了一个新项目。

当我分配了CALayer的内容时:

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

我收到错误


非目标的隐式转换ARC指令类型'CGImageRef'到'id'不允许使用ARC

Implicit conversion of a non-Objective-C pointer type 'CGImageRef' to 'id' is disallowed with ARC

只需转换 CGImageRef id 隐藏错误,但我想知道ARC是否仍能正常运行?

Simply casting the CGImageRef to id hides the error, but I was wondering if the ARC still functions correctly then?

推荐答案

您应该从WWDC 2011中查看ARC视频。它们可以在开发者网站上找到并通过iTunes打开。特别是:

You should really check out the ARC videos from WWDC 2011. They are available on the developer site and open through iTunes. Especially:


•会话323 - 自动引用计数简介

• Session 323 – Introducing Automatic Reference Counting

•会话322 - Objective-C深度推进

• Session 322 – Objective-C Advancements in Depth

此外,ARC参考注释:

Also, the ARC reference notes:

https ://developer.apple.com/library/content/releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html

参考笔记和视频都讨论了Core Graphics(等)以及它们如何与ARC协同工作。

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"


在许多Cocoa应用程序中,您需要使用Core Foundation样式的对象,无论是来自Core Foundation框架本身(例如
CFArrayRef还是CFMutableDictionaryRef),还是来自采用
的框架核心基础约定,如Core Graphics(您可能使用类型
,如CGColorSpaceRef和CGGradientRef)。

In many Cocoa applications, you need to use Core Foundation-style objects, whether from the Core Foundation framework itself (such as CFArrayRef or CFMutableDictionaryRef) or from frameworks that adopt Core Foundation conventions such as Core Graphics (you might use types like CGColorSpaceRef and CGGradientRef).

编译器不会自动管理Core $ b $的生命周期b基础对象;您必须按照Core
基础内存管理规则的规定调用CFRetain和CFRelease(或
对应的特定类型变体)(参见内存管理编程
Core Foundation指南)。

The compiler does not automatically manage the lifetimes of Core Foundation objects; you must call CFRetain and CFRelease (or the corresponding type-specific variants) as dictated by the Core Foundation memory management rules (see Memory Management Programming Guide for Core Foundation).

如果你在Objective-C和Core Foundation风格的对象之间进行转换,你需要告诉编译器有关对象
的所有权语义。 cast(在objc / runtime.h中定义)或Core
基础样式宏(在NSObject.h中定义):[...]

If you cast between Objective-C and Core Foundation-style objects, you need to tell the compiler about the ownership semantics of the object using either a cast (defined in objc/runtime.h) or a Core Foundation-style macro (defined in NSObject.h): [...]

JörgJacobsen对桥接选项也有一个很好的总结概述:

Jörg Jacobsen has a good summary overview of the bridging options as well: Managing Toll-free Bridging in an ARC’ed Environment.


__ bridge_retained (nb:仅在从对象指针转换为C类型指针时使用它):我(程序员)需要引用这个对象
一段时间在C型指针的黑暗世界中,对于
你是不透明的,ARC。所以,请不要释放这个对象,而我仍然需要
。我(程序员)承诺在我完成它的时候自己发布它(在黑暗的
世界中)

__bridge_retained (n.b.: only use it when casting from object pointer to C type pointer): I (the programmer) need to reference this object for some time in the dark world of C type pointers which is opaque to you, ARC. So please, please do not release this object while I still need it. I (the programmer) promise to release it myself (in the dark world) when I’m done with it

__ bridge_transfer (nb:从C类型指针转换为对象指针时只使用它):我(程序员)交给你,ARC,我拥有的
对象,我不再对黑暗中感兴趣b $ b对你来说不透明的C型指针世界。每当你使用该对象完成
时,请自行释放,因为你知道
正确的时间,从而节省了一些不必自己做的工作。

__bridge_transfer (n.b.: only use it when casting from C type pointer to object pointer): I (the programmer) hand over to you, ARC, an object that I own and that I am no longer interested in in the dark world of C type pointers that is opaque to you. Whenever you, ARC, are done with that object please release it yourself, because you know the right time and thus save me some work not having to do it myself.

__ bridge :ARC,你保持平衡你的保留和释放,因为我在C型指针的黑暗世界中保持平衡,
是......每当我需要在黑暗的世界中抓住一个物体时,我将自己保留它并在适当时释放它。我不需要任何
的额外合同,ARC。

__bridge: ARC, you keep balancing out your retains and releases as I keep balancing out mine in the dark world of C type pointers which is…. Whenever I need to hold on to an object in the dark world I will retain it myself and release it when appropriate. I don’t need any extra contract with you, ARC.

这篇关于ARC使用Core Graphics对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆