0

在ARC下,是否可以使用NSCodingCGMutablePathRef(或其不可变形式)进行编码/解码?天真的我尝试:使用ARC编码和解码CGMutablePathRef

path = CGPathCreateMutable(); 
... 
[aCoder encodeObject:path] 

,但我从编译器得到一个友好的错误:

Automatic Reference Counting Issue: Implicit conversion of an Objective-C pointer to 'CGMutablePathRef' (aka 'struct CGPath *') is disallowed with ARC 

我能做些什么来编码呢?

+0

[CGPathRef编码](http://stackoverflow.com/questions/1429426/cgpathref-encoding) – yuji 2012-03-30 08:08:21

回答

1

NSCoding是一个协议。其方法只能用于符合NSCoding协议的对象。一个CGPathRef甚至不是一个对象,所以NSCoding方法不会直接工作。这就是你得到这个错误的原因。

Here's a guy谁想出了一种序列化CGPaths的方法。

+0

的可能的重复我继续前进,并转换到使用'UIBezierPath',虽然我可以使用其中一个包装CGPath 'UIBezierPath'类方法。 – 2012-03-30 12:21:19

0

如果您要求持久存储CGPath,您应该使用CGPathApply函数。检查here如何做到这一点。

1

您的问题不是由于ARC,而是由于基于C的Core Graphics代码与基于Objective-C的NSCoding机制之间的不匹配。

要使用编码器/解码器,您需要使用符合Objective-C NSCoding协议的对象。 CGMutablePathRef不符合,因为它不是一个Objective-C对象,而是一个核心图形对象引用。

但是,UIBezierPath是一个CGPath的Objective-C包装,它符合。

你可以做到以下几点:

CGMutablePathRef mutablePath = CGPathCreateMutable(); 
// ... you own mutablePath. mutate it here... 
CGPathRef persistentPath = CGPathCreateCopy(mutablePath); 
UIBezierPath * bezierPath = [UIBezierPath bezierPathWithCGPath:persistentPath]; 
CGPathRelease(persistentPath); 
[aCoder encodeObject:bezierPath]; 

,然后解码:

UIBezierPath * bezierPath = [aCoder decodeObject]; 
if (!bezierPath) { 
    // workaround an issue, where empty paths decode as nil 
    bezierPath = [UIBezierPath bezierPath]; 
} 
CGPathRef path = [bezierPath CGPath]; 
CGMutablePathRef * mutablePath = CGPathCreateMutableCopy(path); 
// ... you own mutablePath. mutate it here 

这工作在我的测试。