iphone - 如果原始 CGPoints 丢失,如何将存储的 CGPath 重绘为贝塞尔路径

标签 iphone objective-c ios

如果未存储原始 CGPoints,如何将存储的 CGPath 重绘为贝塞尔路径?

这是代码,但它不起作用(在标准模式而不是贝塞尔模式下重绘路径):

CGMutablePathRef UpathFREEHAND = CGPathCreateMutable();
CGPoint firstPointFH = [[pointArray objectAtIndex:0] CGPointValue];
CGPathMoveToPoint(UpathFREEHAND, NULL, firstPointFH.x, firstPointFH.y);

for (int i = 0; i < [pointArray count]; i++)
    {
        CGPathAddLineToPoint(UpathFREEHAND, NULL, [[pointArray objectAtIndex:i] CGPointValue].x, 
        [[pointArray objectAtIndex:i] CGPointValue].y);
    }

CGMutablePathRef _TempPathForUndo = UpathFREEHAND;

//Add PATH object to array
[UPath addObject:CFBridgingRelease(_TempPathForUndo)];

//Load PATH object from array
_TTempPathForUndo = (__bridge CGMutablePathRef)([UPath objectAtIndex:i]);

// Now create the UIBezierPath object.
UIBezierPath *bp;
bp = [UIBezierPath bezierPath];
bp.CGPath = _TTempPathForUndo;

CGContextAddPath(context, bp.CGPath);
//Color, Brush Size parameters, Line cap parameters..
CGContextStrokePath(context);

最佳答案

您可以使用UIBezierPath中的以下方法:

+ (UIBezierPath *)bezierPathWithCGPath:(CGPathRef)CGPath

重用CGPath是有效的。检查此示例,在 UIViewController 中添加 TestView 并链接 UIButton,以在使用 [_testView setNeedsDisplay]< 单击它时强制重绘。/:

// TestView.h

#import <UIKit/UIKit.h>

@interface TestView : UIView {

    CGMutablePathRef _path;
    BOOL _nextDraws;
}

@end

// TestView.m

#import "TestView.h"

@implementation TestView

- (void)drawRect:(CGRect)rect {

    BOOL firstDraw = !_nextDraws;
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    if (firstDraw) {
        NSLog(@"first draw");

        _path = CGPathCreateMutable();
        CGPathMoveToPoint(_path, NULL, 0, 0);
        CGPathAddLineToPoint(_path, NULL, CGRectGetMaxX(rect), CGRectGetMaxY(rect));
        CGPathCloseSubpath(_path);
        CGContextAddPath(ctx, _path);
        CGContextSetStrokeColorWithColor(ctx,[UIColor whiteColor].CGColor);
        CGContextStrokePath(ctx);

        _nextDraws = YES;
    }
    else {
        NSLog(@"next draws");

        CGContextRef ctx = UIGraphicsGetCurrentContext();
        CGContextClearRect(ctx, rect);
        UIBezierPath * bezierPath = [UIBezierPath bezierPathWithCGPath:_path];
        CGContextAddPath(ctx, bezierPath.CGPath);
        CGContextSetStrokeColorWithColor(ctx,[UIColor whiteColor].CGColor);
        CGContextStrokePath(ctx);
    }
}

- (void)dealloc {
    CGPathRelease(_path);
    [super dealloc];
}

@end

关于iphone - 如果原始 CGPoints 丢失,如何将存储的 CGPath 重绘为贝塞尔路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12995555/

相关文章:

iphone - UIButton - 更改框架会改变对触摸的响应

iphone - iPhone OS4 (XCode 3.2.3) 上的 setFetchLimit 和sectionNameKeyPath 导致崩溃

ios - 访问全局类

ios - 如何使我的 ios app sdk 向后兼容

iOS:当 UITextField 获得焦点时,show segue 阻止 UITableViewController 自动滚动

iPhone 在核心数据中保存多个对象?

iOS:在更改 View 之前设置属性

objective-c - 正态分布最佳方法

objective-c - 是否能够重写 NSObject init 方法以将每个对象添加到单个 NSMutableArray 中?

ios - 两部iPhone的branch.io链接行为不同