【问题标题】:Getting Blur Strokes while drawing in iOS在 iOS 中绘图时获得模糊笔触
【发布时间】:2012-07-13 07:27:45
【问题描述】:

我正在 iOS 中进行绘图,我的代码工作正常,绘图工作正常,现在我已经实现了撤消和重做功能,但是发生了什么,假设我画了两条线,然后按撤消按钮,然后绘制的第一条线变得模糊,我不明白为什么会这样,下面是我的撤消代码。

-

(void)redrawLine
{
    UIGraphicsBeginImageContext(self.bounds.size);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    NSDictionary *lineInfo = [lineArray lastObject];
    curImage = (UIImage*)[lineInfo valueForKey:@"IMAGE"];
    UIGraphicsEndImageContext();
    [self setNeedsDisplayInRect:self.bounds];
    [self setNeedsDisplay];
}



-(void)undoButtonClicked
{
    if([lineArray count]>0){
        NSMutableArray *_line=[lineArray lastObject];
        [bufferArray addObject:[_line copy]];
        [lineArray removeLastObject];
        drawStep = UNDO;
        [self redrawLine];
    }

}


- (void)drawRect:(CGRect)rect
{

    switch (drawStep) {
        case DRAW:
        {
            [curImage drawAtPoint:CGPointMake(0, 0)];
            CGPoint mid1 = midPoint(previousPoint1, previousPoint2); 
            CGPoint mid2 = midPoint(currentPoint, previousPoint1);
            CGContextRef context = UIGraphicsGetCurrentContext(); 

            [self.layer renderInContext:context];
            CGContextMoveToPoint(context, mid1.x, mid1.y);
            CGContextAddQuadCurveToPoint(context, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y); 
            CGContextSetLineCap(context, kCGLineCapButt);
            CGContextSetLineWidth(context, self.lineWidth);
            CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
            CGContextSetAlpha(context, self.lineAlpha);
            CGContextSetAllowsAntialiasing(context, YES);
            CGContextStrokePath(context);            
            [super drawRect:rect];  

        }        
        case UNDO:
        {
            [curImage drawInRect:self.bounds];   
            break;
        }
}

下图是按下撤消按钮时发生的情况

第一张图片是在点击撤销按钮之前,第二张图片是在点击撤销按钮之后

问候 兰吉特

【问题讨论】:

    标签: ios cocoa-touch drawing core-graphics quartz-graphics


    【解决方案1】:

    您对UIGraphicsBeginImageContext 的调用是以点为单位指定位图的大小(即全屏为 320x480),但在具有视网膜显示器的设备上,您需要一个以像素为单位的屏幕大小(即 640x960)。

    您应该能够调用 UIGraphicsBeginImageContextWithOptions 并将 scale 设置为 0.0。来自文档:

    应用于位图的比例因子。如果您指定值 0.0,则比例因子设置为设备主屏幕的比例因子。

    【讨论】:

    • 嗨马丁非常感谢,它解决了这个问题,如何确定比例因子?这个问题也出现在 ipod touch 上(没有视网膜显示)。
    • 谢谢,伙计,我有同样不想要的模糊,但这个解决方案可以解决它!
    • 巨大的帮助!没想到这个!谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多