Est-il possible d'get tous uibezerpath de uiview?

J'ai fait des searchs et trouvé aucune réponse.

Est-il possible d'get une fois UIBezierPath de UIView une fois dessiné dans UIView si j'utilise CGGontext pour le dessiner à la vue? (Si oui, pouvez-vous me donner une idée de comment?) Le UIBezierPath est venu de l'input de l'user, donc si l'user dessine beaucoup de UIBezierPath, j'ai besoin de tous ces paths et l'save dans un file .svg.

Voici un meilleur exemple de travail, après avoir testé certaines des réponses que j'ai données dans mes commentaires, c'est celui qui returnnera un tableau de shapelayers, ou couches selon ce que vous cherchez:

CAShapeLayer * firstNameCorners = [CAShapeLayer layer]; [firstNameCorners setPath:[UIBezierPath bezierPathWithRoundedRect:CGRectMake(100, 100, 196.75, 44.0) byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerTopLeft cornerRadii:(CGSize){2.5, 2.5}].CGPath]; [[self.view layer] addSublayer:firstNameCorners]; NSMutableArray *this = [[NSMutableArray alloc] init]; for (CALayer * rd in self.view.layer.sublayers) { if ([rd isKindOfClass:[CAShapeLayer class]]) { [this addObject:rd]; NSLog(@"this is a layer: %@", rd); } } NSLog(@"this is an Array of Shapes: %@", this); 

CAShapeLayer peut également stocker une seule ligne, peu importe la complexité de la ligne, cet object peut la stocker si elle provient d'un UIBezierPath et que vous la stockez dans les sous-couches de l'UIView sur lequel vous dessinez. Et en fait, pour développer cela davantage, et pour vos objectives voir ce qui suit, cela returnnera le CGPath qui, selon votre question, est ce que vous vouliez:

 CAShapeLayer * firstNameCorners = [CAShapeLayer layer]; [firstNameCorners setPath:[UIBezierPath bezierPathWithRoundedRect:CGRectMake(100, 100, 96.75, 44.0) byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerTopLeft cornerRadii:(CGSize){2.5, 2.5}].CGPath]; [[self.view layer] addSublayer:firstNameCorners]; NSMutableArray *this = [[NSMutableArray alloc] init]; for (CALayer * rd in self.view.layer.sublayers) { if ([rd isKindOfClass:[CAShapeLayer class]]) { [this addObject:rd]; CAShapeLayer * rf = (CAShapeLayer*)rd; NSLog(@"this is a layer: %@", rf.path); CGRect pathBounds = CGPathGetBoundingBox(rf.path); NSLog(@"this is this boundingBox: origin.X: %f, origin.X: %f, size.Width: %f, size.Height: %f", pathBounds.origin.x, pathBounds.origin.y, pathBounds.size.width, pathBounds.size.height); } } NSLog(@"this is an Array of Shapes: %@", this); 

Et, si vous voulez aller encore plus loin, cela fonctionne pour stocker les coordonnées CGRect, et aussi, cela vous montrera comment récupérer et utiliser ces coordonnées:

 CAShapeLayer * firstNameCorners = [CAShapeLayer layer]; [firstNameCorners setPath:[UIBezierPath bezierPathWithRoundedRect:CGRectMake(100, 100, 196.75, 44.0) byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerTopLeft cornerRadii:(CGSize){2.5, 2.5}].CGPath]; [[self.view layer] addSublayer:firstNameCorners]; NSMutableArray *this = [[NSMutableArray alloc] init]; NSMutableArray *that = [[NSMutableArray alloc] init]; for (CALayer * rd in self.view.layer.sublayers) { if ([rd isKindOfClass:[CAShapeLayer class]]) { [this addObject:rd]; CAShapeLayer * rf = (CAShapeLayer*)rd; NSLog(@"this is a layer: %@", rf.path); CGRect pathBounds = CGPathGetBoundingBox(rf.path); [that addObject:[NSValue valueWithCGRect:pathBounds]]; NSLog(@"this is this boundingBox: origin.X: %f, origin.X: %f, size.Width: %f, size.Height: %f", pathBounds.origin.x, pathBounds.origin.y, pathBounds.size.width, pathBounds.size.height); } } 

Maintenant, pour finir, et ceci est une adaptation de https://github.com/erica/iOS-6-Cookbook , et ceci est pour montrer le démêlage complet de mon dessin d'un UIBezierPath pour commencer:

 #define VALUE(_INDEX_) [NSValue valueWithCGPoint:points[_INDEX_]] -(void)showPaths { CAShapeLayer * firstNameCorners = [CAShapeLayer layer]; [firstNameCorners setPath:[UIBezierPath bezierPathWithRoundedRect:CGRectMake(100, 100, 196.75, 44.0) byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerTopLeft cornerRadii:(CGSize){2.5, 2.5}].CGPath]; [[self.view layer] addSublayer:firstNameCorners]; NSMutableArray *this = [[NSMutableArray alloc] init]; NSMutableArray *that = [[NSMutableArray alloc] init]; for (CALayer * rd in self.view.layer.sublayers) { if ([rd isKindOfClass:[CAShapeLayer class]]) { [this addObject:rd]; CAShapeLayer * rf = (CAShapeLayer*)rd; NSLog(@"this is a layer: %@", rf.path); CGRect pathBounds = CGPathGetBoundingBox(rf.path); [that addObject:[NSValue valueWithCGRect:pathBounds]]; CGMutablePathRef p3 = CGPathCreateMutableCopy(rf.path); NSArray *p3points = [self pointsFromCGPath:p3]; for (NSValue *point in p3points) { NSLog(@"path element in p3: %@", NSSsortingngFromCGPoint(point.CGPointValue)); } NSLog(@"this is this boundingBox: origin.X: %f, origin.X: %f, size.Width: %f, size.Height: %f", pathBounds.origin.x, pathBounds.origin.y, pathBounds.size.width, pathBounds.size.height); } } } void getPointsFromBezier(void *info, const CGPathElement *element) { NSMutableArray *bezierPoints = (__bridge NSMutableArray *)info; // Resortingeve the path element type and its points CGPathElementType type = element->type; CGPoint *points = element->points; switch (type) { case kCGPathElementMoveToPoint: NSLog(@"MoveToPoint (%3.2f, %3.2f", points->x, points->y); break; case kCGPathElementAddLineToPoint: NSLog(@"AddLineToPoint (%3.2f, %3.2f)", points->x, points->y); break; case kCGPathElementAddQuadCurveToPoint: NSLog(@"AddQuadCurveToPoint (%3.2f, %3.2f), (%3.2f, %3.2f)", points->x, points->y, points[1].x, points[1].y); break; case kCGPathElementAddCurveToPoint: NSLog(@"AddCurveToPoint (%3.2f, %3.2f), (%3.2f, %3.2f), (%3.2f, %3.2f)", points->x, points->y, points[1].x, points[1].y, points[2].x, points[2].y); break; case kCGPathElementCloseSubpath: NSLog(@"CloseSubpath (%3.2f, %3.2f)", points->x, points->y); break; default: NSLog(@"unknown"); break; } // Add the points if they're available (per type) if (type != kCGPathElementCloseSubpath) { [bezierPoints addObject:VALUE(0)]; if ((type != kCGPathElementAddLineToPoint) && (type != kCGPathElementMoveToPoint)) [bezierPoints addObject:VALUE(1)]; } if (type == kCGPathElementAddCurveToPoint) [bezierPoints addObject:VALUE(2)]; } - (NSArray *)pointsFromCGPath:(CGPathRef)path { NSMutableArray *points = [NSMutableArray array]; CGPathApply(path, (__bridge void *)points, getPointsFromBezier); return points; } 

La sortie se connectera à la console, tous les paths qui sont stockés dans CAShapeLayer avec lesquels nous avons commencé, la sortie est trop grande et trop compliquée pour être publiée ici, mais voici à quoi ressemble le début:

[30146: 2680987] MoveToPoint (103.82, 100.00

[30146: 2680987] AddLineToPoint (296.75, 100.00)

[30146: 2680987] AddLineToPoint (296.75, 144.00)

[30146: 2680987] AddLineToPoint (103.82, 144.00)

[30146: 2680987] AddCurveToPoint (102.72, 144.00), (102.17, 144.00), (101.67, 143.84)

[30146: 2680987] AddLineToPoint (101.58, 143.81)

[30146: 2680987] AddCurveToPoint (100.93, 143.58), (100.42, 143.07), (100.19, 142.42)

[30146: 2680987] AddCurveToPoint (100.00, 141.83), (100.00, 141.28), (100.00, 140.18)

[30146: 2680987] AddLineToPoint (100.00, 103.82)

[30146: 2680987] AddCurveToPoint (100.00, 102.72), (100.00, 102.17), (100.16, 101.67)

[30146: 2680987] AddLineToPoint (100.19, 101.58)

[30146: 2680987] AddCurveToPoint (100.42, 100.93), (100.93, 100.42), (101.58, 100.19)

[30146: 2680987] AddCurveToPoint (102.17, 100.00), (102.72, 100.00), (103.82, 100.00)

[30146: 2680987] AddLineToPoint (103.82, 100.00)

[30146: 2680987] élément path dans p3: {103.8216625, 100}

[30146: 2680987] élément de path dans p3: {296.75, 100}

[30146: 2680987] élément de path dans p3: {296.75, 144}

[30146: 2680987] élément path dans p3: {103.8216625, 144}

[30146: 2680987] élément path dans p3: {102.72123239404632, 144}

[30146: 2680987] élément de path dans p3: {102.17101736015751, 144}

Si vous utilisez Core Graphics pour dessiner les paths, vous avez probablement un model drawRect la vue sur laquelle drawRect afin de dessiner tous les paths. Si vous ne le faites pas (c'est-à-dire que vous ne faites que mettre à jour un bitmap), vous devez simplement refactoriser ce code pour capturer et save les informations nécessaires pour représenter les paths.