是否可以从uiview中获取所有uibezerpath? [英] Is it possible to get all uibezerpath from uiview?

查看:71
本文介绍了是否可以从uiview中获取所有uibezerpath?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究,没有找到答案。

I've been researching and found no answer.

如果我使用CGGontext将其绘制到UIView中,一旦从UIView中绘制,是否有可能从UIView中获取所有UIBezierPath。视图? (如果是,您能给我一个方法的想法吗?)UIBezierPath来自用户输入,因此,如果用户绘制了大量UIBezierPath,则需要所有这些路径并将其保存到.svg文件中。

Is it possible to get all UIBezierPath from UIView once drawn in UIView if I use CGGontext for drawing it to the view? (if yes, can you give me an idea to how?) The UIBezierPath came from user input, so if the user draws a lot of UIBezierPath, I need all those path and save it to a .svg file.

推荐答案

这是一个更好的工作示例,在测试了我在评论中给出的一些答案之后,这是一个将返回一个shapelayer数组,或取决于您要寻找的图层:

Here's a better working example, after testing some of the answers I've given in my commments, this is the one that will return an array of shapelayers, or layers depending on what you are looking for:

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也可以只存储一行,不管行有多复杂,该对象如果它来自UIBezierPath,则可以存储它,并将其存储在要绘制的UIView的子图层中。实际上,为了进一步扩展它,并且出于您的目的,请参见以下内容,这将返回CGPath,根据您的问题,它就是您想要的:

CAShapeLayer can also store just a single line, doesn't matter how complex the line is, this object can store it if it's coming from a UIBezierPath and you store this in the subLayers of the UIView you are drawing on. And in fact to expand this further, and for your purposes see the following, this will return the CGPath which, according to your question, is what you wanted:

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);

此外,如果您想走得更远,则可以存储CGRect坐标, ,这将向您展示如何检索和使用这些坐标:

And, if you want to go even further, then this works to store the CGRect coordinates, and also, this will show you how to retrive and use these coordinates:

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);
    }
}

现在,结束,这是一种改编来自 https://github.com/erica/iOS-6-Cookbook ,是为了显示我对UIBezierPath的绘制的完整说明,以以下内容开头:

Now, to finish off, and this is an adaptation from https://github.com/erica/iOS-6-Cookbook, and this is to show the complete unraveling of my drawing of a UIBezierPath to begin with:

#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: %@", NSStringFromCGPoint(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;

    // Retrieve 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;
}

输出将记录到控制台,所有存储在CAShapeLayer中的路径我们开始时,输出太大且凌乱,无法在此处发布,但这是它的开头:

The output will log to the console, all the paths that are stored in CAShapeLayer we started with, the out put is too big and messy to post here, but here's what the start of it looks like:


[30146 :2680987] MoveToPoint(103.82,100.00

[30146:2680987] MoveToPoint (103.82, 100.00

[30146:2680987] AddLineToPoint(296.75,100.00)

[30146:2680987] AddLineToPoint (296.75, 100.00)

[ 30146:2680987] AddLineToPoint(296.75,144.00)

[30146:2680987] AddLineToPoint (296.75, 144.00)

[30146:2680987] AddLineToPoint(103.82,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] AddCurveToPoint (102.72, 144.00), (102.17, 144.00), (101.67, 143.84)

[30146:2680987] AddLineToPoint( 101.58,143.81)

[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.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] AddCurveToPoint (100.00, 141.83), (100.00, 141.28), (100.00, 140.18)

[30146:2680987] AddLineToPoint(100.00,10 3.82)

[30146:2680987] AddLineToPoint (100.00, 103.82)

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

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

[30146:2680987] AddLineToPoint(100.19,101.58)

[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 (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] AddCurveToPoint (102.17, 100.00), (102.72, 100.00), (103.82, 100.00)

[30146:2680987] AddLineToPoint(103.82,100.00)

[30146:2680987] AddLineToPoint (103.82, 100.00)

[30146:2680987] p3中的路径元素:{103.8216625,100}

[30146:2680987] path element in p3: {103.8216625, 100}

[30146:2680987] p3中的路径元素:{296.75,100}

[30146:2680987] path element in p3: {296.75, 100}

[30146:2680987]路径元素:{296.75,144}

[30146:2680987] path element in p3: {296.75, 144}

[30146:2680987] p3的路径元素:{103.8216625,144}

[30146:2680987] path element in p3: {103.8216625, 144}

p3中的[30146:2680987]路径元素:{102.72123239404632,144}

[30146:2680987] path element in p3: {102.72123239404632, 144}

[30146:2680987] p3中的路径元素:{102.17101736015751,144}

[30146:2680987] path element in p3: {102.17101736015751, 144}

这篇关于是否可以从uiview中获取所有uibezerpath?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆