当方向改变时,核心图形绘制会延伸

时间:2017-03-17 03:46:17

标签: objective-c xcode

我正在创建一个允许您绘制的应用程序,它在纵向模式下运行良好。但是,当我旋转屏幕时,绘制的图像会变形(压扁和拉伸)。即使UIImageView已改变其尺寸,我还需要做些什么来保持图像的尺寸?

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

    mouseSwiped = NO;
    UITouch *touch = [touches anyObject];
    lastPoint = [touch locationInView:self.view]; // first point touched
}

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

    mouseSwiped = YES;
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:self.view]; // second point touched (to be connected to first point)

    // initialize the UIImageView that will be drawn on
    UIGraphicsBeginImageContext(self.view.frame.size);
    //UIGraphicsBeginImageContextWithOptions(self.view.frame.size, NO, 0.0); // use this instead of previous line to make smoother drawings
    if (!erasing) // choose tempDrawImage if not erasing
        [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    else // choose mainImage if erasing
        [self.mainImage.image drawInRect:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];

    // draw a line with CGContextAddLineToPoint from lastPoint to currentPoint
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);

    if (!erasing) { // color selected
        // set brush size, opacity, and stroke color
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush );
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
        CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);

        // draw the path
        CGContextStrokePath(UIGraphicsGetCurrentContext());

        // draw on the tempDrawImage UIImageView
        self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext();
        [self.tempDrawImage setAlpha:opacity];
    }
    else { // eraser selected
        // set brush size
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush);
        CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeClear);

        // draw the path
        CGContextStrokePath(UIGraphicsGetCurrentContext());

        // draw on the tempDrawImage UIImageView
        self.mainImage.image = UIGraphicsGetImageFromCurrentImageContext();
        //[self.mainImage setAlpha:1.0];
    }

    UIGraphicsEndImageContext();
    lastPoint = currentPoint;
}

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

    // if screen was not swiped (i.e. the screen was only tapped), draw a single point
    if(!mouseSwiped) {
        UIGraphicsBeginImageContext(self.view.frame.size);
        //UIGraphicsBeginImageContextWithOptions(self.view.frame.size, NO, 0.0); // use this instead of previous line to make smoother drawings
        if (!erasing) {
            [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
            CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
            CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush);
            CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, opacity);
            CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
            CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
            CGContextStrokePath(UIGraphicsGetCurrentContext());
            CGContextFlush(UIGraphicsGetCurrentContext());
            self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext();
        }
        else {
            [self.mainImage.image drawInRect:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
            CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
            CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush);
            CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeClear);
            CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
            CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
            CGContextStrokePath(UIGraphicsGetCurrentContext());
            CGContextFlush(UIGraphicsGetCurrentContext());
            self.mainImage.image = UIGraphicsGetImageFromCurrentImageContext();
        }
        UIGraphicsEndImageContext();
    }

    ///////BRUSH STROKE IS DONE; BEGIN UIIMAGEVIEW MERGE//////

    if (!erasing) {
        // initialize mainImage (for merge)
        UIGraphicsBeginImageContext(self.mainImage.frame.size);

        // merge tempDrawImage with mainImage
        [self.mainImage.image drawInRect:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) blendMode:kCGBlendModeNormal alpha:1.0];
        [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) blendMode:kCGBlendModeNormal alpha:opacity];
        self.mainImage.image = UIGraphicsGetImageFromCurrentImageContext();

        // clear tempDrawImage
        self.tempDrawImage.image = nil;
    }

    UIGraphicsEndImageContext();
}

0 个答案:

没有答案
相关问题