本文介绍了在iOS上使用ARC将CALayer的内容设置为CGImageRef的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下代码使用手动内存管理进行编译,但在ARC下失败:
The following code compiles fine with manual memory management, but fails under ARC:
CALayer *layer = [CALayer layer];
layer.contents = [[UIImage imageNamed:@"dial.png"] CGImage];
错误是:
如何使用ARC运行
推荐答案
CoreFoundation对象不由ARC管理。 ARC只适用于Objective-C对象,并且不会隐式地知道当从(或到)非对象指针类型转换时转移所有权。
CoreFoundation objets aren't managed by ARC. ARC only works with Objective-C objets and does not implicitly know how to transfer the ownership when casting from (or to) an non-object pointer type.
告诉ARC如何在转换期间传递对象的所有权:
You need to tell ARC how the ownership of the objet is transferred during the cast:
layer.contents = (__bridge id) [[UIImage imageNamed:@"dial.png"] CGImage];
这篇关于在iOS上使用ARC将CALayer的内容设置为CGImageRef的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!