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

Posted slgkaifa

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将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的主要内容,如果未能解决你的问题,请参考以下文章

如何将圆角添加到 UIBezierPath 自定义矩形?

使用 UIBezierPath 将半径设置为某些角并向自定义 UIButton 添加阴影

VBA MS 项目将多个自定义报告以 PDF 格式保存到位置

如何从 UIBezierPath 创建 IBDesignable 自定义 UIView?

Windows项目迁移到Linux总结

如何将xlsx格式转换成html 格式?