Encode and Decode CGMutablePathRef with ARC

In ARC, is it possible to encode / decode a CGMutablePathRef(or its immutable form) with NSCoding? I naively try:

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

but I get a friendly error from the compiler:

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

What can I do to encode this?

0
source share
3 answers

NSCodingis a protocol. Its methods can only be used with objects that conform to the protocol NSCoding. a is CGPathRefnot even an object, so methods NSCodingwill not work directly. That is why you get this error.

, CGPaths.

+1

ARC, Core Graphics , C, NSCoding, Objective-C.

/, , Objective-C NSCoding. CGMutablePathRef , Objective-C, Core Graphics.

UIBezierPath - Objective-C CGPath, .

:

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 

.

+1

CGPath , CGPathApply. , , .

0

Source: https://habr.com/ru/post/1717762/


All Articles