Пътят на Безие в iOS се показва със зигзаг

опитвайки се да науча някои кварцови графики и се опитвам да рисувам гладко. Опитвам се да комбинирам тези 2 урока

http://mobile.tutsplus.com/tutorials/iphone/ios-sdk_freehand-drawing/

http://www.raywenderlich.com/18840/how-to-make-a-simple-drawing-app-with-uikit

И досега това е моят код

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

}

i.imgur.com/qOwihxC.png

Ъглите изглеждаха много по-добре, но самите линии изглеждат така, сякаш имат зигзаг в тях. Ще съм благодарен, ако ме насочите към грешката ми. Благодаря


person user2067051    schedule 17.06.2013    source източник


Отговори (1)


Мисля, че изображението, което рисувате, не е с размер на ретината, защото self.tempImage.frame.size връща размера на рамката в точки, а не в пиксели. След това, когато рисувате изображението на екрана, то е увеличено и пикселизирано. Ако направите буферното изображение да съответства на размера на пикселите на компонента на екрана, тогава трябва да сте добре.

person Sean Reilly    schedule 05.09.2013