首页 > 代码库 > 将UIBezierPath存为自己定义格式的字符串,再将字符串转为UIBezierPath

将UIBezierPath存为自己定义格式的字符串,再将字符串转为UIBezierPath

<pre name="code" class="objc">自己定义字符串格式为:@"123.02,234.23|321.23,432.0002|543.0003,432.0045|654.0034,567.0034|23.3443,56.0034|77.3440,55.3443|"
/**
 *@brief:将BezierPath中的点转为字符串
 */
+ (NSString*)convertBezierPathToNSString:(UIBezierPath*) bezierPath;
{

    NSString *pathString = @"";

    CGPathRef yourCGPath = bezierPath.CGPath;
    NSMutableArray *bezierPoints = [NSMutableArray array];
    CGPathApply(yourCGPath, (__bridge void *)(bezierPoints), MyCGPathApplierFunc);
    
    for (int i = 0; i < [bezierPoints count]; ++i)
    {
     
        CGPoint point = [bezierPoints[i] CGPointValue];
        pathString = [pathString stringByAppendingString:[NSString stringWithFormat:@"%f",point.x]];
        pathString = [pathString stringByAppendingFormat:@"%@",@","];
        pathString = [pathString stringByAppendingString:[NSString stringWithFormat:@"%f",point.y]];
        pathString = [pathString stringByAppendingString:@"|"];

    }

    return pathString;
}


void MyCGPathApplierFunc (void *info, const CGPathElement *element) {
    NSMutableArray *bezierPoints = (__bridge NSMutableArray *)info;
    
    CGPoint *points = element->points;
    CGPathElementType type = element->type;
    
    switch(type) {
        case kCGPathElementMoveToPoint: // contains 1 point
            [bezierPoints addObject:[NSValue valueWithCGPoint:points[0]]];
            break;
            
        case kCGPathElementAddLineToPoint: // contains 1 point
            [bezierPoints addObject:[NSValue valueWithCGPoint:points[0]]];
            break;
            
        case kCGPathElementAddQuadCurveToPoint: // contains 2 points
            [bezierPoints addObject:[NSValue valueWithCGPoint:points[0]]];
            [bezierPoints addObject:[NSValue valueWithCGPoint:points[1]]];
            break;
            
        case kCGPathElementAddCurveToPoint: // contains 3 points
            [bezierPoints addObject:[NSValue valueWithCGPoint:points[0]]];
            [bezierPoints addObject:[NSValue valueWithCGPoint:points[1]]];
            [bezierPoints addObject:[NSValue valueWithCGPoint:points[2]]];
            break;
            
        case kCGPathElementCloseSubpath: // contains no point
            break;
    }
}



<pre name="code" class="objc">
/**
 *@brief:将字符串中的点转为BezierPath
 */
+ (UIBezierPath*)convertNSStringToBezierPath:(NSString*) bezierPathString
{
    
    NSMutableArray *pointsArray = [[NSMutableArray alloc] init];
    NSInteger length = 0;
    
    //解析字符串
    NSString *separatorString1 = @",";
    NSString *separatorString2 = @"|";
    
    NSScanner *aScanner = [NSScanner scannerWithString:bezierPathString];
    
    while (![aScanner isAtEnd]) {
        
        NSString *xString, *yString;
        
        [aScanner scanUpToString:separatorString1 intoString:&xString];
        [aScanner setScanLocation:[aScanner scanLocation]+1];
        
        [aScanner scanUpToString:separatorString2 intoString:&yString];
        [aScanner setScanLocation:[aScanner scanLocation]+1];
        

        CGPoint point;
        point.x = [xString floatValue];
        point.y = [yString floatValue];
        
        [pointsArray addObject:[NSValue valueWithCGPoint:point]];
        
        
    }
    
    
    //首先将字符串解析为CGPoint数组,再将数组初始化UIBeizerPath
    length = [pointsArray count];
    UIBezierPath *bezierPath = [[UIBezierPath alloc] init];
    [bezierPath moveToPoint:[pointsArray[0] CGPointValue] ];
    for (NSUInteger j=1; j< length; j++) {
        [bezierPath addLineToPoint:[pointsArray[j] CGPointValue] ];
    }
    
    return bezierPath;
}



将UIBezierPath存为自己定义格式的字符串,再将字符串转为UIBezierPath