Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Path export study
//
// CAShapeLayer+customShape.m
//
//
// Created by hmiorroi on 5/3/15.
// Copyright (c) 2015 hmiorroi. All rights reserved.
//
#import "CAShapeLayer+customShape.h"
@implementation CAShapeLayer (customShape)
+ (CAShapeLayer*)starIconWithFrame:(CGRect)rect
{
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(34.997, 29.809)];
[path addCurveToPoint:CGPointMake(32.132, 31.89) controlPoint1:CGPointMake(34.51, 30.796) controlPoint2:CGPointMake(33.221, 31.732)];
[path addCurveToPoint:CGPointMake(13.979, 34.528) controlPoint1:CGPointMake(32.132, 31.89) controlPoint2:CGPointMake(13.979, 34.528)];
[path addCurveToPoint:CGPointMake(13.432, 36.212) controlPoint1:CGPointMake(12.891, 34.686) controlPoint2:CGPointMake(12.644, 35.444)];
[path addCurveToPoint:CGPointMake(26.568, 49.016) controlPoint1:CGPointMake(13.432, 36.212) controlPoint2:CGPointMake(26.568, 49.016)];
[path addCurveToPoint:CGPointMake(27.662, 52.383) controlPoint1:CGPointMake(27.356, 49.783) controlPoint2:CGPointMake(27.848, 51.299)];
[path addCurveToPoint:CGPointMake(24.561, 70.463) controlPoint1:CGPointMake(27.662, 52.383) controlPoint2:CGPointMake(24.561, 70.463)];
[path addCurveToPoint:CGPointMake(25.993, 71.503) controlPoint1:CGPointMake(24.375, 71.547) controlPoint2:CGPointMake(25.02, 72.015)];
[path addCurveToPoint:CGPointMake(42.23, 62.967) controlPoint1:CGPointMake(25.993, 71.503) controlPoint2:CGPointMake(42.23, 62.967)];
[path addCurveToPoint:CGPointMake(45.77, 62.967) controlPoint1:CGPointMake(43.203, 62.455) controlPoint2:CGPointMake(44.797, 62.455)];
[path addCurveToPoint:CGPointMake(62.007, 71.503) controlPoint1:CGPointMake(45.77, 62.967) controlPoint2:CGPointMake(62.007, 71.503)];
[path addCurveToPoint:CGPointMake(63.439, 70.463) controlPoint1:CGPointMake(62.98, 72.015) controlPoint2:CGPointMake(63.625, 71.547)];
[path addCurveToPoint:CGPointMake(60.338, 52.383) controlPoint1:CGPointMake(63.439, 70.463) controlPoint2:CGPointMake(60.338, 52.383)];
[path addCurveToPoint:CGPointMake(61.432, 49.016) controlPoint1:CGPointMake(60.152, 51.299) controlPoint2:CGPointMake(60.644, 49.783)];
[path addCurveToPoint:CGPointMake(74.568, 36.212) controlPoint1:CGPointMake(61.432, 49.016) controlPoint2:CGPointMake(74.568, 36.212)];
[path addCurveToPoint:CGPointMake(74.021, 34.528) controlPoint1:CGPointMake(75.356, 35.444) controlPoint2:CGPointMake(75.109, 34.686)];
[path addCurveToPoint:CGPointMake(55.868, 31.89) controlPoint1:CGPointMake(74.021, 34.528) controlPoint2:CGPointMake(55.868, 31.89)];
[path addCurveToPoint:CGPointMake(53.003, 29.809) controlPoint1:CGPointMake(54.779, 31.732) controlPoint2:CGPointMake(53.49, 30.796)];
[path addCurveToPoint:CGPointMake(44.885, 13.36) controlPoint1:CGPointMake(53.003, 29.809) controlPoint2:CGPointMake(44.885, 13.36)];
[path addCurveToPoint:CGPointMake(43.115, 13.36) controlPoint1:CGPointMake(44.398, 12.373) controlPoint2:CGPointMake(43.602, 12.373)];
[path addCurveToPoint:CGPointMake(34.997, 29.809) controlPoint1:CGPointMake(43.115, 13.36) controlPoint2:CGPointMake(34.997, 29.809)];
[path moveToPoint:CGPointMake(88, 44)];
[path addCurveToPoint:CGPointMake(44, 88) controlPoint1:CGPointMake(88, 68.301) controlPoint2:CGPointMake(68.301, 88)];
[path addCurveToPoint:CGPointMake(0, 44) controlPoint1:CGPointMake(19.699, 88) controlPoint2:CGPointMake(0, 68.301)];
[path addCurveToPoint:CGPointMake(44, 0) controlPoint1:CGPointMake(0, 19.699) controlPoint2:CGPointMake(19.699, 0)];
[path addCurveToPoint:CGPointMake(88, 44) controlPoint1:CGPointMake(68.301, 0) controlPoint2:CGPointMake(88, 19.699)];
CGFloat wRatio = rect.size.width / 88.0f;
CGFloat hRatio = rect.size.height / 88.0f;
CGAffineTransform scale = CGAffineTransformMakeScale(wRatio, hRatio);
[path applyTransform:scale];
CAShapeLayer *star = [CAShapeLayer layer];
star.frame = rect;
star.path = path.CGPath;
star.fillRule = kCAFillRuleEvenOdd;
return star;
}
@end
//
// Path export script for Adobe Illustrator
//
// Created by hmiorroi on 5/3/15.
// Copyright (c) 2015 hmiorroi. All rights reserved.
//
var pathName = "path";
var digit = 1000;
var doc = app.activeDocument;
var pathItems = doc.selection;
var itemsLen = pathItems.length;
var bounds = doc.artboards[doc.artboards.getActiveArtboardIndex()].artboardRect;
$.writeln(makeOriginalSize(bounds))
$.writeln(makeBezierPath())
for(var i = 0 ; i < itemsLen ; i++)
{
var pathItem = pathItems[i];
var isClosed = pathItem.closed;
var pathPoints = pathItem.pathPoints;
var pathLen = pathPoints.length;
var endPoint;
for(var j=0; j < pathLen; j++)
{
var ap = roundPoint(convertDToA(pathPoints[j].anchor), digit);
var ld = roundPoint(convertDToA(pathPoints[j].leftDirection), digit);
var c = (j != 0) ? j - 1 : pathLen - 1;
var rd = roundPoint(convertDToA(pathPoints[c].rightDirection), digit);
if (j == 0) {
$.writeln(makeMoveToPoint(ap));
if (isClosed) {
endPoint = makeAddCurve(ap, ld, rd);
}
} else {
$.writeln(makeAddCurve(ap, ld, rd));
}
}
if (endPoint) {
$.writeln(endPoint);
}
}
$.writeln(makeClosePath());
function makeClosePath(){
return 'path.close()';
}
function makeBezierPath(){
return 'var path = UIBezierPath()';
}
function makeOriginalSize(bounds){
var width = Math.abs(bounds[2] - bounds[0]);
var height = Math.abs(bounds[3] - bounds[1]);
return 'let originalSize = ' + 'CGSize.init(width: ' + width + ', height: ' + height + ')';
}
function makeMoveToPoint(anchor)
{
return pathName + '.move(to: ' + makeCGPointMake(anchor) + ')';
}
function makeAddCurve(anchor, leftDirection, rightDirection)
{
return pathName + '.addCurve(to: ' + makeCGPointMake(anchor) +
', controlPoint1: ' + makeCGPointMake(rightDirection) +
', controlPoint2: ' + makeCGPointMake(leftDirection) +
')';
}
function makeCGPointMake(point)
{
return 'CGPoint.init(x: ' + point[0] + ', y: ' + point[1] + ')';
}
function roundPoint(point, places)
{
var p = [];
var len = point.length;
for(var i=0; i < len; i++) {
p.push(Math.round(point[i] * places)/places);
}
return p;
}
function convertDToA(point)
{
var doc = app.activeDocument;
var pos = doc.convertCoordinate (point, CoordinateSystem.DOCUMENTCOORDINATESYSTEM, CoordinateSystem.ARTBOARDCOORDINATESYSTEM);
pos[1] *= -1;
return pos;
}
//
// UIImage+shapeRender.m
//
//
// Created by hmiorroi on 5/3/15.
// Copyright (c) 2015 hmiorroi. All rights reserved.
//
#import "UIImage+shapeRender.h"
@implementation UIImage (shapeRender)
+ (UIImage *)imageWithShapeLayer:(CAShapeLayer *)shapeLayer
{
UIGraphicsBeginImageContextWithOptions(shapeLayer.frame.size, NO, [UIScreen mainScreen].scale);
[shapeLayer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
@end

mitolog commented Aug 5, 2015

Thanks, it's very useful. I modified pathExport.js for supporting swift extension, though it's a bit difference with yours.

https://gist.github.com/mitolog/3bc99de3bcd63c712051

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment