在iOS上使用ARC将CALayer的内容设置为CGImageRef [英] Setting the contents of a CALayer to a CGImageRef on iOS with ARC on
问题描述
以下代码使用手动内存管理进行编译,但在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
Automatic Reference Counting Issue: Implicit conversion of a non-Objective-C pointer type 'CGImageRef' (aka 'struct CGImage *') to 'id' is disallowed with ARC
如何使用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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!