Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading a CGpath/UIBezierPath from a remote location

Is there any feasible/reasonable way to pass the necessary path info for a UIBezierPath from a remote API? I'm interested in downloading shape information at runtime. Here's a dream-code example of what I mean:

CGPath path = [APIClient downloadPathInfoForObject:clock];
UIBezierPath *bPath = [UIBezierPath bezierPathWithCGPath:path];

How would you send path info from an API, and subsequently unpack it to a CGPath?

I'll probably end up using regular images, but it would be cool to use CAShapeLayers throughout the app. Any ideas?

like image 925
Kyle Truscott Avatar asked Dec 07 '25 09:12

Kyle Truscott


1 Answers

UIBezierPath supports NSCoding so you should be able to serialize it with NSKeyedArchiver, send it and deserialize it with NSKeyedUnarchiver into another UIBezierPath at the other location.

Example

// Get a base64 encoded archive of the path
UIBezierPath *path = UIBezierPath.path;
NSData *pathData = [NSKeyedArchiver archivedDataWithRootObject:path];    
NSString *pathString = [pathData base64Encoding]; // Save it to the API

// ...... sometime in the future

NSString *pathString = [APIClient downloadPathForObject:clock]; // Get the string
NSData *pathData = [[NSData alloc] initWithBase64Encoding:pathString];
UIBezierPath *path = [NSKeyedUnarchiver pathData]; // Same path!
like image 131
zaph Avatar answered Dec 10 '25 00:12

zaph