iOS贝塞尔路径以之字形显示

iOS贝塞尔路径以之字形显示,ios,drawing,quartz-graphics,Ios,Drawing,Quartz Graphics,尝试学习一些石英图形,并尝试做一个平滑的绘图。我试图结合这两个教程 到目前为止,这是我的代码 #import "ViewController.h" @interface ViewController () { UIBezierPath *path; CGPoint pts[5]; uint ctr; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad

尝试学习一些石英图形,并尝试做一个平滑的绘图。我试图结合这两个教程

到目前为止,这是我的代码

#import "ViewController.h"

@interface ViewController ()
{
    UIBezierPath *path;
    CGPoint pts[5];
    uint ctr;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.


    [self setupData];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



-(void) setupData
{
    [self.mainImage setMultipleTouchEnabled:NO];
    [self.tempImage setMultipleTouchEnabled:NO];
    path = [UIBezierPath bezierPath];
    [path setLineWidth:2.0];

}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    ctr = 0;
    UITouch *touch = [touches anyObject];
    pts[0] = [touch locationInView:self.tempImage];
}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint p = [touch locationInView:self.tempImage];
    ctr++;
    pts[ctr] = p;
    if (ctr == 4)
    {
        pts[3] = CGPointMake((pts[2].x + pts[4].x)/2.0, (pts[2].y + pts[4].y)/2.0); // move the endpoint to the middle of the line joining the second control point of the first Bezier segment and the first control point of the second Bezier segment
        [path moveToPoint:pts[0]];
        [path addCurveToPoint:pts[3] controlPoint1:pts[1] controlPoint2:pts[2]]; // add a cubic Bezier from pt[0] to pt[3], with control points pt[1] and pt[2]

        [self draw];

        [self.mainImage setNeedsDisplay];
        [self.tempImage setNeedsDisplay];
        // replace points and get ready to handle the next segment
        pts[0] = pts[3];
        pts[1] = pts[4];
        ctr = 1;
    }
}



- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{

    [path removeAllPoints];
    ctr = 0;


    UIGraphicsBeginImageContext(self.tempImage.frame.size);
    [self.mainImage.image drawInRect:CGRectMake(0, 0, self.tempImage.frame.size.width, self.tempImage.frame.size.height) blendMode:kCGBlendModeNormal alpha:1.0];
    [self.tempImage.image drawInRect:CGRectMake(0, 0, self.tempImage.frame.size.width, self.tempImage.frame.size.height) blendMode:kCGBlendModeNormal alpha:1.0];
    self.mainImage.image = UIGraphicsGetImageFromCurrentImageContext();
    self.tempImage.image = nil;

    [self.tempImage setNeedsDisplay];
    [self.mainImage setNeedsDisplay];

    UIGraphicsEndImageContext();



}

- (void)draw
{
    UIGraphicsBeginImageContext(self.tempImage.frame.size);
    [self.tempImage.image drawInRect:CGRectMake(0, 0, self.tempImage.frame.size.width, self.tempImage.frame.size.height)];

    pts[3] = CGPointMake((pts[2].x + pts[4].x)/2.0, (pts[2].y + pts[4].y)/2.0); // move the endpoint to the middle of the line joining the second control point of the first Bezier segment and the first control point of the second Bezier segment
    [path moveToPoint:pts[0]];
    [path addCurveToPoint:pts[3] controlPoint1:pts[1] controlPoint2:pts[2]]; // add a cubic Bezier from pt[0] to pt[3], with control points pt[1] and pt[2]
    [path stroke];

    CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);

    //    CGContextStrokePath(UIGraphicsGetCurrentContext());
    self.tempImage.image = UIGraphicsGetImageFromCurrentImageContext();
    [self.tempImage setAlpha:1.0];


    UIGraphicsEndImageContext();

}


角落看起来好多了,但线条本身看起来像是锯齿形的。如果你能指出我的错误,我将不胜感激。谢谢

我认为您正在绘制的图像没有视网膜大小,因为self.tempImage.frame.size返回的帧大小是以点而不是像素为单位的。然后,当您将图像绘制到屏幕上时,它将放大并像素化。如果您使缓冲区图像与屏幕上组件的像素大小匹配,则应该可以。

我认为您正在绘制的图像是非视网膜大小的,因为self.tempImage.frame.size以点而不是像素返回帧大小。然后,当您将图像绘制到屏幕上时,它将放大并像素化。如果您使缓冲区图像与屏幕上组件的像素大小相匹配,则应该可以